【问题标题】:function argument a pointer array (C)函数参数一个指针数组 (C)
【发布时间】:2013-04-12 17:38:37
【问题描述】:

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

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

但是,当我 compile 代码和 run 它时,编译器会发出 warnings 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 *’

【问题讨论】:

  • addToBucket 长什么样子?
  • 我在主题中发布了代码,因为我无法让它在此评论中格式化
  • 不应该 struct Container **bucket[10]){ 是 struct Container **bucket 吗?
  • 该参数假设采用一个指针数组,所以它不应该是 struct Container **bucket[10]

标签: c arrays pointers


【解决方案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));

【讨论】:

    【解决方案2】:

    您需要将 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.

    【讨论】:

    • 对不起,我忘了提:我最初将容器声明为:typedef struct Container container;我只是没有更改方法,我将编辑我的代码以反映这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多