[英]function argument a pointer array (C)
in my code I have a array of pointers, where the pointers point to my struct 在我的代码中,我有一个指针数组,其中的指针指向我的结构
struct Container *bucket[sizeOfHashMap];
I have a function that will return 1 of these array pointers (eg it may return the pointer at array index 6). 我有一个函数,将返回这些数组指针中的1个(例如,它可能返回数组索引6处的指针)。 As an argument it wants a pointer to this pointer. 作为参数,它需要一个指向该指针的指针。 The function can be seen here: 该功能可以在这里看到:
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];
}
I call the function like this (where test is an array of characters) 我这样调用函数(其中test是一个字符数组)
addToBucket(test, getWhichBucket(test, &bucket));
the add to bucket looks like this: 添加到存储桶如下所示:
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;
}
However the compiler issues warnings
when I compile
the code and when I run
it I get a segmentation error
. 但是,当我compile
代码并run
它时,编译器会发出warnings
,但会出现segmentation error
。 Does anyone know why? 有人知道为什么吗? The warnings can be seen here: 警告可以在这里看到:
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 *’
You need to change your declaration of addToBucket
from 您需要从以下位置更改对addToBucket
的声明
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;
}
to 至
void addToBucket(char word[], Container *bucket)
{
Container *temp = malloc (sizeof(struct Container));
strcpy(temp->key, word);
temp->value = 9001;
temp->next = *bucket;
*bucket = temp;
return;
}
Note the change in case for Container
-- case matters in C... container
is not the same thing as Container
. 请注意Container
大小写更改-C ... container
大小写问题与Container
不相同。
Also... note... you should not cast malloc
in C . 另外...注意... 您不应该在C中malloc
。
addToBucket(test, getWhichBucket(test, &bucket));
is passing a 正在通过
struct Container *(*)[10]
to getWhichBucket
. 到getWhichBucket
。 That's the wrong type, as the compiler says. 正如编译器所说,那是错误的类型。
You can fix the prototype and implementation 您可以修复原型和实现
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];
}
or change the call, but there's no easy way to get a struct Container **bucket[10]
from a struct Container *bucket[10]
, so then you'd probably still want to change the type and implementation of getWhichBucket
. 或更改调用,但是没有简单的方法从struct Container *bucket[10]
获取struct Container **bucket[10]
struct Container *bucket[10]
,因此您可能仍想更改getWhichBucket
的类型和实现。
Since you're not modifying the bucket
argument there, there's no need to pass the address, you can simply pass the struct Container *bucket[10]
directly, 由于您无需在此处修改bucket
参数,因此无需传递地址,您只需直接传递struct Container *bucket[10]
,
struct Container* getWhichBucket(char word[], struct Container *bucket[]){
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];
}
and call 并打电话
addToBucket(test, getWhichBucket(test, bucket));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.