Are char * argv[] arguments in main null terminated?
Asked Answered
F

2

55

I'm wondering if command line parameters are always null terminated. Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true?

int main(int argc, char** argv)
{
    char *p;

    for(int cnt=1; cnt < argc; ++cnt)
    {
        p = argv[cnt];
        printf("%d = [%s]\n", cnt, p);
    }
    return 0;
}

$ MyProgram -arg1 -arg2 -arg3
1 = -arg1
2 = -arg2
3 = -arg3
Froggy answered 13/6, 2012 at 17:25 Comment(5)
Think for a minute: what would happen if they weren't NULL terminated?Dumbarton
Thinking for a minute... char[] and char* does not automatically imply a C-style string. Obviously the entire command line string is NULL terminated but that does not have to translate to each each individual array of characters passed on the command line being NULL terminated. It would be easy enough for argv to just strip out the whitespace on the command line and and have non null terminated character arrays and not C style strings.Froggy
@Froggy - if the character arrays were not NULL terminated how could you detect the end of they character array?Jonjona
@Jonjona Ahh, ok I see your point. char* argv[]. If they aren't null terminated how do you know where one starts and the other begins in the 2d array. Got it, I stand corrected, this makes sense now. Thanks.Froggy
@Froggy It's about knowing where each string ends & being able to pass it to every other C func that expects a NUL terminator. It's got nothing to do with the strings being in an array. An array like char* argv[] is not a 2D array; it's an array of pointers. (Surely in a real 2D array, each string would have to be the same length, so we wouldn't need a terminator?) The pointed-to objects do not have to be adjacent to each other. That is: here, it is not required that the address pointed to by each successive pointer must be 1 byte after the NUL terminating the previously numbered argument.Ginsburg
S
94

Yes. The non-null pointers in the argv array point to C strings, which are by definition null terminated.

The C Language Standard simply states that the array members "shall contain pointers to strings" (C99 §5.1.2.2.1/2). A string is "a contiguous sequence of characters terminated by and including the first null character" (C99 §7.1.1/1), that is, they are null terminated by definition.

Further, the array element at argv[argc] is a null pointer, so the array itself is also, in a sense, "null terminated."

Suprematism answered 13/6, 2012 at 17:27 Comment(4)
Can you explain the use of argc if argv is null terminated. Can't we detect no. of arguments by traversing the array until '\0' is reached.. ?Occiput
@Occiput Yes, you can compute argc from argv. As for why main has the signature it has, I do not know for sure. It's been this way since before I existed.Suprematism
@Snehasish: Just for fun, I asked this question on Retrocomputing S.E. -- it's a holdover from pre-ANSI C: retrocomputing.stackexchange.com/questions/5179/…Calculation
Only the last element of the array is a null pointer, but you can have argc == 0 (though it is extremely rare and requires careful setup) so that there are no strings in the argument list. If the command name is not available but there are other arguments, the standard stipulates that argv[0] shall be an empty string, not a null pointer.Michaeu
V
4

Yes, it is always true that the arguments are null terminated strings.

Vining answered 13/6, 2012 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.