【问题标题】:double pointer variable giving access violation error in c++ structure program在 C++ 结构程序中给出访问冲突错误的双指针变量
【发布时间】:2015-01-16 17:26:15
【问题描述】:

我正在尝试使用双指针**通过另一个结构'Dict'访问结构'Word'的成员,但在Visual Studio 2010中出现'访问冲突'错误。我也在stackoverflow上检查了链接“accessing double pointer to structure”但是它也没有解决问题。有人可以帮我识别代码中的错误吗?我在这里内联代码:

=============================================

#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;
}

================================================ =========

【问题讨论】:

  • 你没有让wordArray指向任何东西。但是请放弃使用std::vectors
  • 提示:dic-&gt;wordArray 的值是多少?
  • 在使用指针指向指针之前,您应该先学习如何使用指针。
  • @SunilKumar - I am writing a Dictionary code in c++ 你不知道如何使用new[] 创建(和销毁)动态二维数组?暂时忘掉结构吧——你能写一个简单的 main() 程序来创建一个动态二维数组吗?

标签: c++ double-pointer


【解决方案1】:

也许你应该放下双指针并做类似的事情:

 struct Word{
    WordType type;
    char word[20];
    Word* next;
};


struct Dict{
    int size;
    int capacity;
    Word *word;
};

主要是:

dic->word = new Word;
dic->word.next = nullptr;

strcpy(dic->word->word,"hi");

然后使用下一个指针使单词成为链表。

编辑:不能使用上述解决方案,因为原始结构应保持不变。

也许可以试试:

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 创建的一切。

【讨论】:

  • 尼尔森。感谢您的评论。这肯定会奏效,但我的任务只需要使用双指针,我别无选择。
  • @Kumar:所以你不能改变任何结构?
  • 或者你可以使用 C++ 成语。
  • 是的尼尔森。我不允许更改我正在使用的结构,并且必须使用相同的方式。
  • 我需要更多帮助。您能帮我理解“Word *[MAX_NUMBER_OF_WORDS]”的含义吗?因为它对我来说是全新的,我在任何地方都找不到任何帮助。提前致谢。
【解决方案2】:

查看您的代码,我看不出为什么 struct Dict 中的 Word 应该是双指针。

但我看到了一个很好的理由,为什么 struct Word 中的 char 应该是一个双指针 - 它是一个“字数组”或 char 的二维矩阵。

所以我提出了这个可行的修改......

#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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多