1

在我的代码中,我有一个指针数组,指针指向我的结构

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 *’
4

2 回答 2

1
addToBucket(test, getWhichBucket(test, &bucket));

正在通过一个

struct Container *(*)[10]

getWhichBucket. 正如编译器所说,这是错误的类型。

您可以修复原型和实现

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

或更改调用,但没有简单的方法struct Container **bucket[10]从 a 获取 a struct Container *bucket[10],因此您可能仍想更改getWhichBucket.

由于您没有在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];
}

并打电话

addToBucket(test, getWhichBucket(test, bucket));
于 2013-04-12T17:44:12.763 回答
1

您需要更改您的声明addToBucketfrom

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

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

请注意Container,C 中的 case 的变化 ...container与. 不同Container

另外......注意......你不应该在 C中投射malloc

于 2013-04-12T17:43:35.380 回答