的數組,我需要建立一個數組來pNodes的指針,但是當我宣佈它,我不知道該陣列創建指針
允許的長度明白我的意思
這是節點結構
typedef struct _Node {
struct _Node* next;
pElement element;
} Node, *pNode;
這是哈希結構
typedef struct _Hash {
int hashSize;
pNode *hashTable;
}Hash,*pHash;
現在我想每個的個hashTable中框指向一個pNode
的問題是,我不知道該數組的大小,如果我沒有它會像(我猜)
pNode hashTable[hashSize]
的方式,我寫的,並試圖到resett所有箱子爲NULL:
這是代碼:
分配內存:
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
的FUNC:
static void resetHashTable(pNode *hashTable, int size) {
int i;
for (i = 0; i < size; i++) {
hashTable[i] = (pNode)NULL;
}
}
的很多很多錯誤,我得到的程序(第一個錯誤)
hash.c:37:18: warning: assignment from incompatible pointer type [enabled by default]
hash->hashTable = (pNode)(malloc(sizeof(pNode) * size));
我能得到一些指點我需要怎麼寫的呢?
您正在將'malloc'的結果轉換爲'pNode',然後嘗試將其分配給'hashTable',它是'pNode *'。 – Michael
你是否在pNode和pHash的聲明中得到一個額外的逗號警告? (你應該) –