0

I am trying to access a member of structure 'Word' through another structure 'Dict' using double pointer ** but getting 'access violation' error in visual studio 2010. I checked link "accessing double pointer to structure" also on stackoverflow but its also not resolving the issue. Can somebody please help me identifying the error in the code? I am inlining the code here:

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

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

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

Community
  • 1
  • 1
  • 4
    You haven't made `wordArray` point to anything. But drop the ponters and use `std::vectors`. – juanchopanza Jan 16 '15 at 17:27
  • 2
    Hint: what is the value of `dic->wordArray`? – barak manos Jan 16 '15 at 17:30
  • 1
    [A pointer is not an array](http://stackoverflow.com/questions/1641957) – Drew Dormann Jan 16 '15 at 17:34
  • `Dict *dic = new Dict;;` Why are you creating a pointer and dynamically creating a `Dict` here? The place where it makes more sense (`wordArray`), you didn't use `new`.. – PaulMcKenzie Jan 16 '15 at 17:39
  • 1
    You should start with learning how to use pointers before you engage pointers to pointers. – molbdnilo Jan 16 '15 at 17:42
  • juanchopanza, thanks for replying on my post..but I have to code like this only and cant use vectors.. Please tell me how can I make wordArray to point something. – Sunil Kumar Jan 16 '15 at 17:43
  • No. But it will be easier for others to answer if you state your requirements in the question. People can't guess that you're supposed to write the code as if it were C. – juanchopanza Jan 16 '15 at 17:47
  • Google the `new` keyword for c++. Also add in there some searching about `constructors`. – Fantastic Mr Fox Jan 16 '15 at 17:49
  • I am writing a Dictionary code in c++ and this line where I am using ** is giving access violation error. So. rather than posting whole code. I have posted simplifies one block code. – Sunil Kumar Jan 16 '15 at 17:52
  • 1
    @SunilKumar - `I am writing a Dictionary code in c++` And you aren't able to figure out how to create (and destroy) a dynamic 2-dimensional array using `new[]`? Forget about the struct for a moment -- can you write a simple main() program to create a dynamic 2-d array? – PaulMcKenzie Jan 16 '15 at 17:55

2 Answers2

0

Maybe you should drop the double pointer and make something like:

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


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

and in main:

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

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

and then make the word a linked list using next pointers.

EDIT: The above solution can't be used as the original structs shall be unchanged.

Maybe try something like:

Dict *dic = new Dict;;
dic->size=0;
dic->capacity=MAX_NUMBER_OF_WORDS;
dic->wordArray=new Word *[dic->capacity];

and when inserting new words:

dic->wordArray[dic->size] = new Word;
dic->size++;

And add a check for capacity versus size to avoid overflow.

BTW: Remember to use delete and everything created by new.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

Looking at your code, I see no reason why Word in struct Dict should be a double pointer.

But I see a very good reason why char inside struct Word should be a double pointer - it is a "word array" or a 2D matrix of char.

So I propose this modification which works...

#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;
    }
dspfnder
  • 1,135
  • 1
  • 8
  • 13