Pointer and struct array manipulation in C -


 #define max_keys 65  struct key {          int id;         char cryptkeys[max_keys]; };    int main(int argc, char ** argv) { int max_line = 69; struct key *table[3]; struct key *(*p)[] = &table; //allocating space pointers in array for(int = 0; < 4; i++) {         table[i] = malloc(sizeof(struct key)); } //parsing id , keys char id[3]; char key[65];    for(int = 0; < size-1; i++) {         struct key *k = (struct key*)malloc(sizeof(struct key));         string = a[i];         strncpy(id, string, 3);         id[3] = '\0';         k->id = atoi(id);         for(int j = 4; j < strlen(string); j++) {                 key[j-4] = string[j];         }         strcpy(k->cryptkeys, key);         table[i] = k;          printf("%s", table[i]->cryptkeys); //this print } for(int = 0; < sizeof(table) -1; i++) {         printf("%d", table[i]->id); //seg fault here, difference above?         printf(" ");         printf("%s", table[i]->cryptkeys);  } return 0; } 

hi everyone, had question manipulating pointers in c. have declared array of pointers filled structs have created. each struct accepts int , string value read in file. question editing values inside of array, assigning new values , accessing values in there. assign values after parsing them file, segmentation fault when try print them out below. why code keep segfault in last loop, have print out values in array of pointers differently would? thank you!

sizeof(table) not 3. it's 24 3*8 (number of array elements*size of address). segmentation fault because try access table[3] (and on) not allocated.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -