函数参数指针数组(C)

时间:2013-04-12 17:38:38

标签: c arrays pointers

在我的代码中我有一个指针数组,其中指针指向我的struct

struct Container *bucket[sizeOfHashMap];

我有一个函数将返回这些数组指针中的一个(例如,它可能会返回数组索引为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 *’

2 个答案:

答案 0 :(得分:1)

您需要从

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

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中的案例事项containerContainer不同。

另外......注意...... you should not cast malloc in C

答案 1 :(得分: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]获取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));