1

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 (e.g it may return the pointer at array index 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)

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. 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 *’
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
zidsal
  • 577
  • 1
  • 7
  • 30

2 Answers2

1

You need to change your declaration of addToBucket from

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.

Also... note... you should not cast malloc in C.

Community
  • 1
  • 1
K Scott Piel
  • 4,320
  • 14
  • 19
  • sorry I forgot to mention: I orginally declared container as this: typedef struct Container container; I just didn't change the methods over I'll edit my code to reflect this. – zidsal Apr 12 '13 at 17:44
1
addToBucket(test, getWhichBucket(test, &bucket));

is passing a

struct Container *(*)[10]

to 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.

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,

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));
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431