#include <stdio.h>
int main(int argc, char* argv[]){
printf("argc: %d\n",argc);
for(int i=0;i<sizeof(argv);i++){
printf("argv[%d] %s\n",i,argv[i]);
}
return(0);
}
compiles fine, when using it under a gnome terminal under a GNU/linux distribution
printTest one\ two three
argc: 3
argv[0] /data/local/tmp/printTest
argv[1] one two
argv[2] three
argv[3] (null)
I intentionally left a white space escaped and argv seems to behave really strangely.
Is this behaviour normal ? What is that null ? why argv creates a null pointer instead of just providing a shorter array ?
sizeof(argv)is meaningless — it’s the size of a pointer (4 on your platform), which just coincidentally happens to be one more than the number of values inargvin this case.Use
argchere instead.