来自“char”的转换无效;到“char*”使用strcpy
好的,以下是我遇到问题的代码部分:
char * historyArray;
historyArray = new char [20];
//get input
cin.getline(readBuffer, 512);
cout << readBuffer <<endl;
//save to history
for(int i = 20; i > 0; i--){
strcpy(historyArray[i], historyArray[i-1]); //ERROR HERE//
}
strcpy(historyArray[0], readBuffer); //and here but it's the same error//
我收到的错误是:
"invalid conversion from 'char' to 'char*'
initializing argument 1 of 'char* strcpy(char*, const char*)'
该项目是创建一个 psudo OS Shell,它将捕获和处理中断以及运行基本的 unix 命令。我遇到的问题是我必须将过去 20 个命令存储到在堆栈上动态分配的字符数组中。 (并且还取消分配)
当我只使用二维字符数组时,上面的代码工作正常:
char historyArray[20][];
但问题是它不是动态的......
是的,我确实知道 strcpy 应该用于复制字符串。
任何帮助将不胜感激!
Ok so here are the parts of my code that I'm having trouble with:
char * historyArray;
historyArray = new char [20];
//get input
cin.getline(readBuffer, 512);
cout << readBuffer <<endl;
//save to history
for(int i = 20; i > 0; i--){
strcpy(historyArray[i], historyArray[i-1]); //ERROR HERE//
}
strcpy(historyArray[0], readBuffer); //and here but it's the same error//
The error that i'm receiving is:
"invalid conversion from 'char' to 'char*'
initializing argument 1 of 'char* strcpy(char*, const char*)'
The project is to create a psudo OS Shell that will catch and handle interrupts as well as run basic unix commands. The issue that I'm having is that I must store the past 20 commands into a character array that is dynamically allocated on the stack. (And also de-allocated)
When I just use a 2d character array the above code works fine:
char historyArray[20][];
but the problem is that it's not dynamic...
And yes I do know that strcpy is supposed to be used to copy strings.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
historyArray
指向 20 个char
的数组(的第一个元素)。您只能在该数组中存储一个字符串。在 C 中,您可以创建一个 char** 对象并让它指向 char* 对象数组的第一个元素,其中每个元素都指向一个字符串。这就是
main()
的argv
参数的作用。但由于您使用的是 C++,因此使用
string
的vector
并让库为您进行内存管理更有意义。historyArray
points to (the first element of) an array of 20char
s. You can only store one string in that array.In C, you could create a
char**
object and have it point to the first element of an array ofchar*
objects, where each element points to a string. This is what theargv
argument tomain()
does.But since you're using C++, it makes a lot more sense to use a
vector
ofstring
s and let the library do the memory management for you.停止在 C++ 程序中使用 C 习惯用法:
因此,我们:
Stop using C idioms in a C++ program:
As a result, we have:
两种解决方案。第一个是如果您出于某种原因确实想要数组,则更推荐使用另一个,并且使用
std::string
更“C++”。然后,您可以对
historyArray
中的元素使用strcpy
。建议我重复的第二个解决方案(我修复了其他一些问题):
Two solutions. The first is if you for some reason really want arrays, the other is more recommended and more "C++"ish using
std::string
s.Then you can use
strcpy
on the elements inhistoryArray
.Second solution which I repeat is recommended (I've fixed a few other things):
HistoryArray[i] 是一个字符。这是一个单一的字符。你想用刺。您的基本问题是historyArray是一个
char*
,这意味着它指向包含字符的内存范围。您希望它是一个char**
,它是一个指向字符串的指针。你的初始化代码是historyArray[i] is a char. It is a single character. You want to use a sting. Your fundemental problem is that historyArray is a
char*
which means that it points to a memory range containing characters. You want it to be achar**
which is a pointer to a pointer to a string. Your initialization code would be错误 1:索引超出了数组范围,i 设置为 20。
错误 2:historyArray[i] 是 char,而不是 char *。您需要&historyArray[i]。
Error 1: You're indexing past your array bounds with i being set to 20.
Error 2: historyArray[i] is a char, not a char *. You need &historyArray[i].
数组表示法提供引用,而 strcopy 需要指针。使用取址 (&) 运算符将引用转换为指针。
Array notation gives references while strcopy wants pointers. Convert references to pointers with address-of (&) operator.
但这只能修复编译器错误,而不能修复代码中的逻辑错误。您使用 C++ 所以字符串解决方案:
或者,如果您想要一个包含 readBuffer 中所有内容的长字符串:
例如...
But that will only fix the compiler errors, not the logical errors in the code. Your using C++ so the string solution:
Alternatively if you want one long string containing everything from readBuffer:
For example...