无法比较或指定指针 [英] Can't compare or assign pointers
本文介绍了无法比较或指定指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
using namespace std;
int main()
{
char *STR[] = {"e", "es"};
char *SOL[] = {"e", "es"};
*SOL[0] = &STR[1];
}
使用上面的代码,我期望SOL的第一个值索引来获取STR的第二个索引的内存地址。我打算这样做从文件加载字节并将它们与另一个文件进行比较以进行验证。这对于仿真方面等是必要的。
Using the above code, I expect SOL's first valued index to obtain the memory address of STR's second index. I plan to do this to load bytes in from a file and compare them to another file for verification. This is necessary for emulation aspects, etc.
推荐答案
问题有点不清楚 - 目标是什么?
你已经声明了一个数组指向字符串的指针。由于指针和数组在c ++中有很多共同点,因此有许多方法可以将数组1中索引1所指向的字符串分配给数组2中的索引0.可以使用指针或数组来完成。
Question is a bit unclear - what is the goal?
You have declared an array of pointers to character strings. As pointers and arrays have a lot in common in c++ there are a number of ways you can assign the string pointed to at index 1 in array 1 to index 0 in array 2. It can be done using either pointers or arrays.
#include <iostream>
using namespace std;
int main()
{
char *STR[] = {"e0", "es0"};
char *SOL[] = {"e1", "es1"};
//#define ARRAY
#ifdef ARRAY
SOL[0] = STR[1]; // Treat as array
#else
*(SOL+0) = *(STR+1); // Treat as pointer.
#endif
cout << SOL[0] << endl;
cout << SOL[1] << endl;
return 0;
}
这篇关于无法比较或指定指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文