C++ const char* To const char* const

不想你离开。 提交于 2019-12-12 11:26:20

问题


I am currently writing an assignment for my class that is supposed to act as a very basic shell. I am nearly finished, but I am running into an issue with execvp and my character array of parameters. Here is a light snippet of my code.

//Split the left content args
istringstream iss(left);
while(getline(iss, s, ' ')){
     v.push_back(s);
}

//Get the split string and put it into array
const char* cmd_left[v.size()+1];
for(unsigned int i = 0; i < v.size(); i++){
     cmd_left[i] = v.at(i).c_str();
}
cmd_left[v.size()] = 0;
v.clear();

And this is utilized by...

execvp(cmd_left[0], cmd_left);

My error is

assign3.cxx:96:34: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]

I understand that the problem is that my character array isn't full of constant data, so I need to essentially go from const char* to const char* const. I read something about const_cast, but I wasn't sure if that is what I need to be doing.

If you would be so kind, could you help me get my array of character arrays to be properly accepted by that function? If you need me to post more of my code, let me know.

Thanks


回答1:


The problem is you cannot pass const variable to function expecting non-const argument.

other word, const char * is a subset of char *.

remove the const

/*const*/ char* cmd_left[v.size()+1];

add const_cast here

cmd_left[i] = const_cast<char *>( v.at(i).c_str() );

other parts of your code look suspicious, but this will make it compile




回答2:


Without any const_cast:

istringstream iss(left);
while(getline(iss, s, ' ')){
     v.push_back(s);
}

//assuming v is not empty! which you were already
string command = v[0]; //store the command in a separate variable (this makes a copy of the string)

char* cmd_left[v.size()+1]; //not a (const char)*
for(unsigned int i = 0; i < v.size(); i++){
     cmd_left[i] = new char[v[i].size()+1];
     strcpy(cmd_left[i], v[i].c_str()); //copy contents of each string onto a new buffer
}
cmd_left[v.size()] = NULL;

v.clear(); //if you really want to; not necessary from the code you posted

//...
execvp(command.c_str(), cmd_left);



回答3:


It is not easy, sometimes not possible to create a const dynamic array of elements because all the elements have to declared within the initializer {}. But luckily you could tell the compiler that the array you are passing is going to be const at least for the certain duration. You could do the following this would yield

&((char* const) (const_cast<char*>(cmd_left[0]) ))

The const_cast inside would remove the const-ness of the array of characters std::string is owning. So, it is quite possible that function might change the contents of array of characters behind the back of std::string. When behaviour of functions taking such argument is known then this might be ok.

If you want to create a const array of char* without resorting to const_cast or managing memory using new/delete, you could use std::vector > instead of vector of strings.

istringstream iss(left);
while(getline(iss, s, ' ')){
     v.push_back(std::vector<char>(s.length()+1));
     strcpy(&v.back().front(),s.c_str());
}

//Get the split string and put it into array
char* cmd_left[v.size()+1];
for(unsigned int i = 0; i < v.size(); i++){
     cmd_left[i] = &v.at(i).front();
}
cmd_left[v.size()] = 0;
v.clear();
execvp(cmd_left[0], &((char* const)cmd_left[0]));

Hope this helps.



来源:https://stackoverflow.com/questions/15961715/c-const-char-to-const-char-const

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!