在C++结构程序中给出访问错误的双指针变量
我试图使用双指针**通过另一个结构“Dict”访问结构“Word”的成员,但在visual studio 2010中出现“访问冲突”错误。我也检查了stackoverflow上的链接,但它也不能解决问题。有人能帮我识别代码中的错误吗?我在这里内联代码: ============================================在C++结构程序中给出访问错误的双指针变量,c++,double-pointer,C++,Double Pointer,我试图使用双指针**通过另一个结构“Dict”访问结构“Word”的成员,但在visual studio 2010中出现“访问冲突”错误。我也检查了stackoverflow上的链接,但它也不能解决问题。有人能帮我识别代码中的错误吗?我在这里内联代码: ============================================ #include <iostream> #include <stdlib.h> #include <time.h> //
#include <iostream>
#include <stdlib.h>
#include <time.h>
//#include "dict.h"
using namespace std;
enum WordType{All, Animal, Fruit, Name};
struct Word{
WordType type;
char word[20];
};
struct Dict{
int size;
int capacity;
Word **wordArray;
};
int main() {
Dict *dic = new Dict;;
dic->size=0;
dic->capacity=0;
strcpy((dic->wordArray[0])->word,"hi");
cout<< (dic->wordArray[0])->word;
system("pause");
return 0;
}
=============================================================也许你应该放下双指针,做出如下动作:
#include <iostream>
#include <stdlib.h>
#include <time.h>
//#include "dict.h"
using namespace std;
enum WordType{All, Animal, Fruit, Name};
struct Word{
WordType type;
char word[20];
};
struct Dict{
int size;
int capacity;
Word **wordArray;
};
int main() {
Dict *dic = new Dict;;
dic->size=0;
dic->capacity=0;
strcpy((dic->wordArray[0])->word,"hi");
cout<< (dic->wordArray[0])->word;
system("pause");
return 0;
}
struct Word{
WordType type;
char word[20];
Word* next;
};
struct Dict{
int size;
int capacity;
Word *word;
};
Dict *dic = new Dict;;
dic->size=0;
dic->capacity=MAX_NUMBER_OF_WORDS;
dic->wordArray=new Word *[dic->capacity];
总的来说:
dic->word = new Word;
dic->word.next = nullptr;
strcpy(dic->word->word,"hi");
然后用下一个指针把这个词做成一个链表
编辑:上述解决方案不能使用,因为原始结构应保持不变
也许可以尝试以下方式:
struct Word{
WordType type;
char word[20];
Word* next;
};
struct Dict{
int size;
int capacity;
Word *word;
};
Dict *dic = new Dict;;
dic->size=0;
dic->capacity=MAX_NUMBER_OF_WORDS;
dic->wordArray=new Word *[dic->capacity];
插入新词时:
dic->wordArray[dic->size] = new Word;
dic->size++;
并添加容量与大小的检查,以避免溢出
顺便说一句:记住使用delete和new创建的所有内容。查看您的代码,我看不出为什么struct Dict中的Word应该是双指针 但我看到了一个很好的理由,为什么struct Word中的char应该是一个双指针——它是一个单词数组或char的2D矩阵 所以我提出这个修改,它是有效的
#include <iostream>
#include <vector>
#include <complex>
#include <iostream>
#include <stdlib.h>
#include <time.h>
//#include "dict.h"
using namespace std;
enum WordType{ All, Animal, Fruit, Name };
struct Word{
WordType type;
char** word;
};
struct Dict{
int size;
int capacity;
Word wordArray;
};
int main() {
Dict *dic = new Dict;
dic->size = 0;
dic->capacity = 0;
dic->wordArray.word = new char*[4]; // make an array of pointer to char size 4
for (int i = 0; i < 10; i++) {
dic->wordArray.word[i] = new char[5]; // for each place of above array, make an array of char size 5
}
strcpy_s(dic->wordArray.word[0], strlen("hi") + 1, "hi");
strcpy_s(dic->wordArray.word[1], strlen("John") + 1, "John");
strcpy_s(dic->wordArray.word[2], strlen("and") + 1, "and");
strcpy_s(dic->wordArray.word[3], strlen("Mary") + 1, "Mary");
cout << dic->wordArray.word[0] << endl;
cout << dic->wordArray.word[1] << endl;
cout << dic->wordArray.word[2] << endl;
cout << dic->wordArray.word[3] << endl;
system("pause");
return 0;
}
你还没有让wordArray指向任何东西。但是抛开思考并使用STD::VoCTRES.HUNT: DIC—WordDARY的价值是什么?你应该先学习如何在指针指向指针之前使用指针。@ SunilKumar -我正在用C++编写字典代码,你无法想出如何用NeX[]创建和销毁动态二维数组。暂时忘掉struct,你能写一个简单的主程序来创建一个动态的二维数组吗?尼尔森。谢谢你的评论。这肯定是可行的,但是我的作业只需要使用双指针,我没有其他选择。@库马尔:所以你不允许改变任何结构?或者你可以使用C++习语。是的,Nielsen。我不允许改变我正在使用的结构,我必须以同样的方式使用。只是我需要更多的帮助。你能帮我理解单词的意思吗。因为这对我来说是全新的,我在任何地方都找不到任何帮助。提前谢谢。