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 parameter to uppercase.
I know that you shouldn't modify string literals as it can cause undefined behavior, so was just wondering if the strings in the *argv[]
are literals that you shouldn't change.
int main(int argc, char *argv[])
From the C11 standard draft N1570, §5.1.2.2.1/2:
They are modifiable. That means they are not string literals.
But be careful: the upper citation only refers to pointers to strings, excluding the obligatory null pointer at
argv[argc]
1.From the C11 standard draft N1570, §5.1.2.2.1/2 (same as above)1:
Notes:
Something regarding this sentence:
"can"? It does always. Undefined behavior includes expected, as if well-defined, and unexpected behavior.
1 Thanks to @black!
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.
On my machine, calling that program with
one two three
arguments produces