0

I need to create an array to pointers of pNodes but when i declare it i dont know the length of the array

lets see what i mean

this is the Node struct

typedef struct _Node {
    struct _Node* next;
    pElement element;
} Node, *pNode;

this is the Hash struct

typedef struct _Hash {
    int hashSize;
    pNode *hashTable;
}Hash,*pHash;

now i want each of the hashTable boxes to point to a pNode

the problem is that i dont know the size of the array, if i did it would be like (i guess)

pNode hashTable[hashSize]

the way i wrote it and tried to resett all boxes to NULL:

this is the CODE:

allocation memory:

pHash hash = (pHash)(malloc(sizeof(Hash)));
hash->hashTable = (pNode)(malloc(sizeof(pNode) * size));
hash->hashSize = size;
resetHashTable(hash->hashTable, size); // reseting the array to NULLS

the func:

static void resetHashTable(pNode *hashTable, int size) {
    int i;
    for (i = 0; i < size; i++) {
        hashTable[i] = (pNode)NULL;
    }
}

one of the many many errors i get from the program is (the first error)

hash.c:37:18: warning: assignment from incompatible pointer type [enabled by default]
  hash->hashTable = (pNode)(malloc(sizeof(pNode) * size));

can i have some pointers how i need to write it?

2
  • 3
    You're casting the result of malloc to a pNode, and then try to assign it to hashTable which is a pNode*. Commented Dec 19, 2014 at 13:21
  • 1
    Do you get a warning about the extra comma in your declarations of pNode and pHash? (you should) Commented Dec 19, 2014 at 13:21

2 Answers 2

4

If this is not C++ just don't cast malloc, you have an error in this line

hash->hashTable = (pNode)(malloc(sizeof(pNode) * size));

It could be

hash->hashTable = (pNode *)(malloc(sizeof(pNode) * size));
                    //   ^ hashTable is declared pNode *

A better solution would be

hash->hashTable = malloc(sizeof(pNode) * size);
Sign up to request clarification or add additional context in comments.

Comments

3

You are declared the pNode as a pointer. Then in Hash structure You are declared the pNode * hastable So you have to use the double pointer **. Or else make that as single pointer in hash structure.

 hash->hashTable = (pNode*)(malloc(sizeof(pNode) * size));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.