char* const*

I am writing a simple shell and I'm getting an error on the execvp function:

error: cannot convert `char*' to `char* const*' for argument `2' to `int execvp(const char*, char* const*)'

I know a const char* is a changeable pointer to a constant char, and char* const is a constant pointer to a changeable char, but I'm not sure what char* const* is. the variable I am using is not constant. I thought that a non-constant variable can be used in a function that requires a constant variable but not the other way around. If anyone could shed some light on this I would appreciate it.
Last edited on
read pointer declarations backwards, and pronouce the * as "pointer to"

So char* const * is a "pointer to a const pointer to a char"

This means:

1
2
3
4
5
char* const* p = ...;

p = foo;  // OK, p is a non-const pointer to a const pointer to a char
*p = foo;  // ERROR, *p is a const pointer to a char
**p = foo;  // OK, **p is a non-const char 
Thanks for clearing that part up, but do you have any idea why I am getting the error?

1
2
3
char* tokens;
//get input and use strtok to tokenize the buffer
execvp(tokens[0], tokens);


error: invalid conversion from `char' to `const char*'
error: cannot convert `char*' to `char* const*' for argument `2' to `int execvp(const char*, char* const*)'
I don't know what execvp is or does.

But the error is just what it says. For the second param, the function takes a char* const* (ie: a pointer to a pointer) and you're only giving it a char* (a pointer).

Reread the docs and make sure you know what the params are doing. The 2nd parameter is probably supposed to be an array of strings and not just a single string.
execvp requires that the pointer you pass in won't change, but tokens can change. It, tokens that is, needs to be const.
Thanks guys! I fixed the error by doing this:

1
2
3
4
5
6
7
char** tokens;
//get input
tokens = new char*[buffer.size() + 1];
strcpy(*tokens, buffer.c_str());
*tokens = strtok(*tokens, " ");
//do some more stuff including fork()
execvp(tokens[0], tokens);
Topic archived. No new replies allowed.