#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
register int i, nflg;
nflg = 0;
if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') {
nflg++;
argc--;
argv++; //Incements a constant pointer, how???
}
for(i=1; i<argc; i++) {
fputs(argv[i], stdout);
if (i < argc-1)
putchar(' ');
}
if(nflg == 0)
putchar('\n');
exit(0);
}
This program increments the value of argv, but argv is a constant pointer in C. Why don’t I get a compilation error from this?
First of all, the type of
argvischar **(remember that in the context of a function parameter declaration,T a[]is synonymous withT *a). Thus it’s a pointer type, not an array type, so use of the++operator is not immediately disallowed.Secondly, while this looks like old-style K&R C, it’s still considered valid. Here’s what the C99 standard (n1256) says about
argcandargv:So the expression
argv++is perfectly legal.