C++ 从';char';至';字符*';使用strcpy

C++ 从';char';至';字符*';使用strcpy,c++,char,strcpy,C++,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

好的,下面是我的代码中有问题的部分:

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//
该项目将创建一个psudo操作系统外壳,它将捕获和处理中断,并运行基本的unix命令。我遇到的问题是,我必须将过去的20条命令存储到堆栈上动态分配的字符数组中。(以及取消分配)

当我只使用2d字符数组时,上面的代码工作正常:

char historyArray[20][];
但问题是它不是动态的

是的,我知道strcpy应该用来复制字符串


任何帮助都将不胜感激

错误1:索引超出数组边界,i设置为20


错误2:historyArray[i]是字符,而不是字符*。您需要&historyArray[i]。

historyArray
指向由20个字符组成的数组(第一个元素)。在该数组中只能存储一个字符串

在C语言中,您可以创建一个
char**
对象,并使其指向
char*
对象数组的第一个元素,其中每个元素指向一个字符串。这就是
main()
argv
参数所做的

但由于使用C++,使用<代码>矢量 <代码>字符串 S,让库为您做内存管理更为合理。

strcpy(&historyArray[i], &historyArray[i-1]);

数组表示法提供引用,而strcopy需要指针。将引用转换为具有(&)运算符地址的指针。

两种解决方案。第一种方法是,如果出于某种原因确实需要数组,那么另一种方法更推荐使用
std::string
s,并且更像“C++”

char * historyArray[20]; // Create an array of char pointers

// ...

historyArray[i] = new char[SIZE]; // Do this for each element in historyArray
然后可以对
historyArray
中的元素使用
strcpy

我重复的第二个解决方案是推荐的(我已经解决了一些其他问题):

字符串历史数组[20];
getline(cin,readBuffer);//将readbuffer也设置为std::字符串

couthistoryArray[i]是一个字符。这是一个单一的字符。你想用刺针。基本问题是historyArray是一个
char*
,这意味着它指向包含字符的内存范围。您希望它是一个
char**
,它是指向字符串指针的指针。您的初始化代码将是

char** historyArray;
historyArray = new char* [20];
for (int i = 0; i < 20; i++)
{
    historyArray[i] = new char [512];  //Big enough to have a 512 char buffer copied in
}
char**historyArray;
historyArray=新字符*[20];
对于(int i=0;i<20;i++)
{
historyArray[i]=新字符[512];//足够大,可以复制512字符缓冲区
}

在C++程序中停止使用C习语:

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();
std::deque历史数组;
//获取输入
字符串读取缓冲区;
std::getline(std::cin,readBuffer);
标准::cout

例如…

这并不能解决问题。他需要存储多个字符串,但他只有一个字符数组。将
&
运算符添加到参数将尝试复制具有未定义行为的重叠字符串。是的,你是对的。我把问题看得太快了。但在他将historyArray转换为指针数组或使用向量后,他仍然需要修复我列出的错误。Keith是正确的。不过,很好地抓住了出界的机会。请注意,他确实访问了
historyArray[0]
两次。他不是指1而不是0,而是指19而不是20。谢谢!我的教授对项目作业的措辞让全班大多数人相信他希望我们使用原生C字符串。我和他谈过,他说他不在乎。不客气。请帮自己一个忙——在将来的课堂作业或其他学习练习中提出的问题中,请加上“家庭作业”标签。这样做有助于像我这样的人不要脱口而出答案(就像我在这里做的那样)。在“家庭作业”标记的问题中,人们会更加注意解释他们的逻辑和推理,而不仅仅是提供答案。
char** historyArray;
historyArray = new char* [20];
for (int i = 0; i < 20; i++)
{
    historyArray[i] = new char [512];  //Big enough to have a 512 char buffer copied in
}
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();
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, readBuffer); //and here but it's the same error//
vector<string> history;

cin.getline(readBuffer,512);

history.push_back(readBuffer);
string history;

cin.getline(readBuffer,512);
history = history += string(readBuffer);