Is this code correct?
char *argv[] = { \"foo\", \"bar\", NULL };
Yes, your code is formally correct (see Steve's remark about const
though). It will produce an array that is terminated with a null pointer of type char *
.
You can also do
char *argv[4] = { "foo", "bar" };
or
char *argv[10] = { "foo", "bar" };
if your array for some reason has to have a specific size. In this case the extra elements will also be set to null pointers, even though you are not initializing them explicitly. But I'd say that even in this case it is better to use
char *argv[4] = { "foo", "bar", NULL };
because that will make sure that the array is indeed long enough to get null-terminated (if the array happens to be too short, the compiler will generate a diagnostic message).