在我的代码中,我有一个指针数组,指针指向我的结构
struct Container *bucket[sizeOfHashMap];
我有一个函数将返回这些数组指针中的 1 个(例如,它可能返回数组索引 6 处的指针)。作为参数,它需要一个指向此指针的指针。该功能可以在这里看到:
struct Container* getWhichBucket(char word[], struct Container **bucket[10]){
int value = 0;
int i = 0;
int size = strlen(word);
int hashIndex = 0;
for(i =0; i < size; i++){
value += (int)word[i];
}
//size of array is worked out by getting memory that array takes up / a slot
hashIndex = value % sizeOfHashMap;
return *bucket[hashIndex];
}
我这样调用函数(其中 test 是一个字符数组)
addToBucket(test, getWhichBucket(test, &bucket));
添加到存储桶如下所示:
void addToBucket(char word[], container **bucket){
container *temp = (struct Container*)malloc (sizeof(struct Container));
strcpy(temp->key, word);
temp->value = 9001;
temp->next = *bucket;
*bucket = temp;
return;
}
但是,warnings
当我compile
编写代码时编译器会出现问题,当run
我得到一个segmentation error
. 有谁知道为什么?可以在此处看到警告:
cw.c: In function ‘main’:
cw.c:86:2: warning: passing argument 2 of ‘getWhichBucket’ from incompatible pointer type [enabled by default]
cw.c:37:19: note: expected ‘struct Container ***’ but argument is of type ‘struct Container * (*)[(long unsigned int)(sizeOfHashMap)]’
cw.c:86:2: warning: passing argument 2 of ‘addToBucket’ from incompatible pointer type [enabled by default]
cw.c:56:6: note: expected ‘struct container **’ but argument is of type ‘struct Container *’