5
4

2 回答 2

4

The problem is that the second parameter of execvp is a char * const *, which is a "pointer to a constant pointer to non-constant data". You're trying to pass it a const char **, which is a "pointer to a pointer to constant data".

The way to fix this is to use char ** instead of const char ** (since "pointer to X" is always allowed to be convert to "pointer to const X", for any type X (but only at the top level of pointers):

char* p[10];
p[0] = ...;
// etc.

Note that if you do need to insert const char * parameters, you can cast them char * as long as you don't modify them. Although the arguments to the exec* family of functions are declared as non-const, they won't ever modify them (see the POSIX 2008 specification). The rationale there explains why they are declared as non-const.

于 2012-10-18T18:25:33.963 回答
4

You can just use char *p[10].

To break it down: char *const *p means "nonconstant pointer to constant pointer of nonconstant char" -- that is, p is writable, p[0] is not writable, and p[0][0] is writable.

于 2012-10-18T18:25:41.007 回答