I just wrote a small program that reads command line arguments in C, nothing too difficult. I was also modifying them, for example changing the first character of the parame
The arrays that support the strings in argv
are modifiable.
But you have no way to know their sizes.
I would frown upon seeing code that (tries to) increase the size of the strings.
#include
#include
// this program may behave erraticaly
int main(int argc, char **argv) {
for (int k = 1; k < argc; k++) {
printf("original argv[%d] is %s\n", k, argv[k]);
}
printf("\n");
for (int k = 1; k < argc; k++) {
strcat(argv[k], " foo"); // add foo to each argv string
printf("first modification to argv[%d] is %s\n", k, argv[k]);
}
printf("\n");
for (int k = argc; k > 1; k--) {
strcat(argv[k - 1], " bar"); // add bar to each argv string
printf("final argv[%d] is %s\n", k - 1, argv[k - 1]);
}
return 0;
}
On my machine, calling that program with one two three
arguments produces
original argv[1] is one original argv[2] is two original argv[3] is three first modification to argv[1] is one foo first modification to argv[2] is foo foo first modification to argv[3] is foo foo final argv[3] is foo foo bar final argv[2] is foo foo foo bar bar final argv[1] is one foo foo foo bar bar bar