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 *’
4
  • What does addToBucket look like? Commented Apr 12, 2013 at 17:40
  • I posted the code in the main topic as I couldn't get it to format nice in this comment Commented Apr 12, 2013 at 17:41
  • Shouldn't struct Container **bucket[10]){ be struct Container **bucket ? Commented Apr 12, 2013 at 17:42
  • the argument is suppose to take an array of pointers so shouldn't it be struct Container **bucket[10] Commented Apr 12, 2013 at 17:43

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.