My professor and a couple of students are arguing about whether argv
is null terminated or not. My friend wrote a small program and it printed out null
but another kid said that he is probably simply reading into blank memory. Can someone solve this discussion?
The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.
argc is the count of arguments, and argv is an array of the strings. The program itself is the first argument, argv[0] , so argc is always at least 1. So, argc is 2 when the program is run with one command-line argument.
The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line.
— argv[argc] shall be a null pointer. The rationale for this is to provide a redundant check for the end of the argument list, on the basis of common practice (ref: Rationale for the ANSI C programming language (1990), 2.1. 2.2).
From the Standard:
5.1.2.2.1 Program startup
...
-- argv[argc] shall be a null pointer.
So, yes; argv is null terminated
According to the standard, "argv[argc] shall be a null pointer" (5.1.2.2.1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With