c - How to access members of a structure in an array? -


this must simple issue, have structure 4 elements in it, 1 structure variable initialized array, problem can access first row of array don't know how access remaining rows...please guide me!

  //structure defined follows         typedef struct{         char first_name[100];         char second_name[100];         int x_position;         int y_position;            } names;      int main(void)  {    int i=0;        //here have initilized structure variable      names my_data[] = {                 {"first", "row",  20, 12},                 {"second", "row", 55, 30},                 {"third",  "row", 80, 47},                 {"fourth", "row", 27, 34}               };      //trying acess diffrent row elements ....but dont know how??     for(i=0; i<=3; i++)     {         printf("%s\n",my_data->first_name);         printf("%s\n",my_data->second_name);         printf("%d\n",my_data->x_position);         printf("%d\n",my_data->y_position);     }        system("pause");         return 0;    } 

in loop correct:

 printf("%s\n", my_data[i].first_name); 

note: precedence of [] array subscript operator higher . member selection via object name operator not need () parenthesis.

or

 printf("%s\n",(my_data + i)->first_name); 

in second, + plus operator has lower precedence need parenthesis overwrite precedence.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -