Is this code correct?
char *argv[] = { \"foo\", \"bar\", NULL };
It's syntactically correct, and it does create a NULL-terminated array of strings.
argv is passed to main
as char*[]
(or equivalently, char**
), but it's "more correct" to treat string literals as a const char*
rather than a char*
. So with this particular example you'd want const char *argv[] = {"foo", "bar", NULL };
Maybe you aren't really going to initialise it with "foo", but actually with a modifiable string that you will want to modify via argv. In that case char*[]
is right. This is the kind of thing Charles probably means by saying that whether code is "correct" depends on what you do with it.