Is this code correct?
char *argv[] = { "foo", "bar", NULL };
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s syntactically correct, and it does create a NULL-terminated array of strings.
argv is passed to
mainaschar*[](or equivalently,char**), but it’s “more correct” to treat string literals as aconst char*rather than achar*. So with this particular example you’d wantconst 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.