4 ms·
"Obviously, if argc == 0, you should not dereference anything in argv." I'd say that's not obvious. Normally argv is null-terminated so you can read argv[argc]
by bloak 3y ago
"Obviously, if argc == 0, you should not dereference anything in argv."
I'd say that's not obvious. Normally argv is null-terminated so you can read argv[argc], and it's zero. There's no need to read argv[argc] if you've already looked at argc but sometimes it's more convenient to rely on the sentinel value.
- phicoh 3y agoIn my opinion that is like saying it might more convenient to use sprintf. Yes, that null has to be there for historical reasons. It doesn't mean it is a good idea to write code that relies on it.
- bloak 3y agoIt seems that (some version of) the C standard contains these words: argv[argc] shall be a null pointer So I wouldn't feel too bad about relying on it.
- lelanthran 3y ago> Yes, that null has to be there for historical reasons. It doesn't mean it is a good idea to write code that relies on it. That null has to be there because if it isn't then the implementation is broken, not the program. If you are coding defensively because the implementation might be broken, then you can't rely on argc being positive either