I am trying to figure out what will be result of passing argv variable to main in this format. main( int argc, char const * argv ).
I know right way of using it is main( int argc, char const **argv) or main( int argc, char const *argv[]).
Here is my code snippet,
#include <stdio.h>
int
main( int argc, char const * argv ) {
for( int i = 0; i < argc; ++i ) {
printf("%s\n", argv[i]);
}
}
Here is output,
$ ./a.exe
..
$ ./a.exe 1
2
....
$ ./a.exe 1 2
2
...
Does it fetch from whatever, argv points? Why does it terminate before even reaching argc.
argv
as a buffer of pointers to non-const strings. - StoryTeller - Unslander Monicaargv[i]
will be achar
value. In theprintf
call, that will be promoted to anint
value. However, theprintf
function is expecting a valid pointer to a null-terminated sequence ofchar
at that position, not some bogus pointer value. - Ian Abbott