使用strcpy从“char”无效转换为“char *” [英] invalid conversion from 'char' to 'char*' using 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//
我收到的错误是: / p>
The error that i'm receiving is:
"invalid conversion from 'char' to 'char*'
initializing argument 1 of 'char* strcpy(char*, const char*)'
该项目是创建一个psudo操作系统Shell,将捕获和处理中断我还要运行基本的unix命令我的问题是,我必须存储过去的20个命令到一个字符数组,动态分配在堆栈上(也解除分配)
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)
当我只使用一个2d字符数组,上面的代码工作正常:
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...
是的,我知道strcpy应该用于复制字符串。
And yes I do know that strcpy is supposed to be used to copy strings.
任何帮助将非常感谢! >
Any help would be greatly appreciated!
推荐答案
在C ++程序中停止使用C成语:
Stop using C idioms in a C++ program:
std::deque<std::string> historyArray;
//get input
std::string readBuffer;
std::getline(std::cin, readBuffer);
std::cout << readBuffer << std::endl;
//save to history
historyArray.push_front(readBuffer);
if(historyArray.size() > 20)
historyArray.pop_back();
因此,我们有:
- 在readBuffer / getline()中没有缓冲区溢出威胁
- 任何地方没有指针可以混淆。
- 没有数组超过
- 任意长的输入字符串
- 经验证的内存分配语义
- No buffer-overflow threat in readBuffer / getline()
- No pointers, anywhere, to confuse us.
- No arrays to overstep the ends of
- Arbitrarily long input strings
- Trivially-proven memory allocation semantics
这篇关于使用strcpy从“char”无效转换为“char *”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!