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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -