pointers - Assigning values error C -


i new c , having trouble pointers , values.

so making hash table , there error insert function (the error posted after code):

typedef struct wordlist {    char *word;    struct wordlist *next; } wordlist;  typedef struct hashtable {    int key;    struct wordlist *value;    struct hashtable *next; } hashtable;  #define table_size 8192  hashtable *table[table_size] = { null };  //insert element void insertelement(int key, char *word) {    int = 0;     // check if key has existed    while((&(table[i]->key) != null) && (i < table_size)) {       // !!!! error here !!!! if(table[i]->key == key) { // if find key              // ... implementation           return; // exit function , skip rest       }        i++; // increment loop index     }     // find null slot , store key , value    if(&(table[i]->key) == null) {       // !!! error here!!! table[i]->key = key;        // ... implementation    } }  int main() {    // test call    insertelement(1, "blah\0");     int i;     ( i=0; < 10; i++)    {       printf("%d: ", i);        struct hashtable *tabletemp = table[i];        while (tabletemp != null)       {          printf("(%d)\n", tabletemp->key);          tabletemp = tabletemp->next;       }        printf("\n");    }     return 0; } 

i use valgrind , error have assignment table[i]-> key = key

invalid write of size 4 address 0x0 not stack'd, malloc'd or (recently) free'd 

in general terms, address 0x0 null pointer; you're attempting write 4-byte value (probably int) using null pointer — like int *p = 0; *p = 1; isn't quite blatant.

in skeletal code, have:

if (&(table[i]->key) == null) {     // !!! error here!!! table[i]->key = key; 

comments:

  1. you should have shown actual code error. have guess because haven't shown it.

  2. you've verified address of table[i]->key null pointer, going lead problems when try assign table[i]->key = key; it.

in future, please provide enough of real code can see mcve (how create minimal, complete, , verifiable example?) or sscce (short, self-contained, correct example) — 2 names , links same basic idea.


Comments