Initializing array of structs in c -
i've initialized array of structs 3 items , it showing 2 me !!!
#include <stdio.h>  typedef struct record {     int value;     char *name; } record;  int main (void) {     record list[] = { (1, "one"), (2, "two"), (3, "three") };     int n = sizeof(list) / sizeof(record);      printf("list's length: %i \n", n);     return 0; }   what happening here? getting crazy?
change initialization to:
record list[] = { {1, "one"}, {2, "two"}, {3, "three"} }; /*                ^        ^  ^        ^  ^          ^  */   your initialization (...) leaves effect similar {"one", "two", "three"} , creates struct array elements { {(int)"one", "two"}, {(int)"three", (char *)0} }
comma operator in c evaluates expressions left right , discard except last. reason why 1, 2 , 3 discarded.
Comments
Post a Comment