来自“char”的转换无效;到“char*”使用strcpy

发布于 2024-12-07 00:27:13 字数 811 浏览 4 评论 0原文

好的,以下是我遇到问题的代码部分:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜柠檬 2024-12-14 00:27:13

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

在 C 中,您可以创建一个 char** 对象并让它指向 char* 对象数组的第一个元素,其中每个元素都指向一个字符串。这就是 main()argv 参数的作用。

但由于您使用的是 C++,因此使用 stringvector 并让库为您进行内存管理更有意义。

historyArray points to (the first element of) an array of 20 chars. 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 of char* objects, where each element points to a string. This is what the argv argument to main() does.

But since you're using C++, it makes a lot more sense to use a vector of strings and let the library do the memory management for you.

半窗疏影 2024-12-14 00:27:13

停止在 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();

因此,我们:

  • readBuffer / getline() 中没有缓冲区溢出威胁
  • 任何地方都没有指针来迷惑我们。
  • 的末尾
  • 没有数组可以超越任意长输入字符串
  • 经过简单验证的内存分配语义

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();

As a result, we have:

  • 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
债姬 2024-12-14 00:27:13

两种解决方案。第一个是如果您出于某种原因确实想要数组,则更推荐使用另一个,并且使用 std::string 更“C++”。

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

// ...

historyArray[i] = new char[SIZE]; // Do this for each element in historyArray

然后,您可以对 historyArray 中的元素使用 strcpy

建议我重复的第二个解决方案(我修复了其他一些问题):

string historyArray[20];

getline(cin, readBuffer); // Make readbuffer an std::string as well
cout << readBuffer << endl;

for(int i = 19; i > 0; i--){ // I think you meant 19 instead of 20
    historyArray[i] = historyArray[i-1];
}

historyArray[0] = readBuffer;

Two solutions. The first is if you for some reason really want arrays, the other is more recommended and more "C++"ish using std::strings.

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

// ...

historyArray[i] = new char[SIZE]; // Do this for each element in historyArray

Then you can use strcpy on the elements in historyArray.

Second solution which I repeat is recommended (I've fixed a few other things):

string historyArray[20];

getline(cin, readBuffer); // Make readbuffer an std::string as well
cout << readBuffer << endl;

for(int i = 19; i > 0; i--){ // I think you meant 19 instead of 20
    historyArray[i] = historyArray[i-1];
}

historyArray[0] = readBuffer;
对不⑦ 2024-12-14 00:27:13

HistoryArray[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
}

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 a char** which is a pointer to a pointer to a string. Your initialization code would be

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
}
執念 2024-12-14 00:27:13

错误 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].

溺深海 2024-12-14 00:27:13
strcpy(&historyArray[i], &historyArray[i-1]);

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

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

Array notation gives references while strcopy wants pointers. Convert references to pointers with address-of (&) operator.

吻安 2024-12-14 00:27:13
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//

但这只能修复编译器错误,而不能修复代码中的逻辑错误。您使用 C++ 所以字符串解决方案:

vector<string> history;

cin.getline(readBuffer,512);

history.push_back(readBuffer);

或者,如果您想要一个包含 readBuffer 中所有内容的长字符串:

string history;

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

例如...

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//

But that will only fix the compiler errors, not the logical errors in the code. Your using C++ so the string solution:

vector<string> history;

cin.getline(readBuffer,512);

history.push_back(readBuffer);

Alternatively if you want one long string containing everything from readBuffer:

string history;

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

For example...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文