sizeof (char) is guaranteed to be 1, but sizeof (char *) is not.
However, your function only works with an error .
For example, try calling it with the following parameters:
abc defg
This will give:
2 args. input: a, c,
which is clearly wrong. The problem is that you are increasing the pointer to the argv element 1 instead of the argv pointer.
Try the following:
#include <stdio.h> int main(int argc, char * argv[]){ char **pi; int i; pi = argv + 1; printf("%d args.\n",argc-1); printf("input: "); for(i=0;i<argc-1;i++){ printf("%c, ",**pi); pi++; } printf("\n"); return 0; }
This will print the first character of each argument:
2 args. input: a, d,
phihag
source share