c - Function to read in a word into a struct array -
i having error code using, wondering if help debug. seems getting malloc error. thanks.
void readwords(char norm_word[maxsize], word ** array) { int = 0; bool found = false; int result = 0; word * current_pointer = malloc (sizeof(word*));//creates temporary variable each pointer in array (i=0; i<word_counter; i++) { current_pointer = *(array+i); //accesses current pointer result = strcmp(norm_word, (current_pointer -> word)); //compares string each stored string if (result == 0) { found = true; (current_pointer->freq)++; break; } } if(!found) { if(pointer_counter == word_counter) { array = realloc(array, sizeof(array)*2); pointer_counter*=2; } word * new_pointer = (word*) malloc (sizeof(word*)); strcpy(new_pointer -> word, norm_word); *(array + (pointer_counter - 1)) = new_pointer; word_counter++; } ; }
all pointers have same size on system. sizeof
returns same size pointer. want allocate structure, need utilize sizeof
on name without star. malloc
homecoming pointer block of memory afterwards.
here short implementation:
#include <iostream> #include <string> typedef struct { int num; int numnum; }numbers; int main(int argc, char ** argv) { numbers* n = (numbers*)malloc(sizeof(numbers)); n->num = 1; n->numnum = 2; free(n); homecoming 0; }
c arrays struct malloc
No comments:
Post a Comment