1

This must be a very simple issue, I have a structure with four elements in it, one structure variable is initialized as an array, now the problem is I can access the first row of the array but I don't know how to access the remaining rows...please guide me!

  //structure is defined as 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 i have initilized structure variable   
  names my_data[] = {
                {"First", "Row",  20, 12},
                {"Second", "Row", 55, 30},
                {"Third",  "Row", 80, 47},
                {"Fourth", "Row", 27, 34}
              }; 
    //trying to acess the 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;
   }
Cătălin Pitiș
  • 14,123
  • 2
  • 39
  • 62
SNJ2000
  • 29
  • 4
  • Read: [Pointer to structure](http://stackoverflow.com/questions/18254623/pointer-to-structure/18254652#18254652) I explained how to use pointer to struct also, helpful in your case I think. – Grijesh Chauhan Aug 20 '13 at 07:53

4 Answers4

5

In loop correct:

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

Note: precedence of [] array subscript operator is higher then . member selection via object name operator so you do not need () parenthesis.

or

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

In second, + plus operator has lower precedence so we need parenthesis to overwrite precedence.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

You need to put the i:-

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

Changed code:-

//structure is defined as 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 i have initilized structure variable

  names my_data[] = {         {"First", "Row",  20, 12},
                              {"Second", "Row", 55, 30},
                              {"Third",  "Row", 80, 47},
                              {"Fourth", "Row", 27, 34}
                                             }; 
    //trying to acess the diffrent row elements ....but dont know how??
    for(i=0; i<=3; i++)
    {
    printf("%s\n",my_data[i]->first_name);
    printf("%s\n",my_data[i]->second_name);
    printf("%d\n",my_data[i]->x_position);
    printf("%d\n",my_data[i]->y_position);
    }

   system("PAUSE");

    return 0;
   }
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0
//structure is defined as follows

#include <stdio.h>

  typedef struct{
        char first_name[100];
        char second_name[100];
        int x_position;
        int y_position;

        }names;


 int main(void)
 {
     int i=0;

   //here i have initilized structure variable

  names my_data[] = {         {"First", "Row",  20, 12},
                              {"Second", "Row", 55, 30},
                              {"Third",  "Row", 80, 47},
                              {"Fourth", "Row", 27, 34}
                                             }; 
    //trying to acess the diffrent row elements ....but dont know how??
    for(i=0; i<=3; i++)
    {
    printf("%s\n",my_data[i].first_name);
    printf("%s\n",my_data[i].second_name);
    printf("%d\n",my_data[i].x_position);
    printf("%d\n",my_data[i].y_position);
    }


    return 0;
   }

you should index the desired item in array; in your example you are using my_data as an pointer that points to my_data[0]

LZR
  • 948
  • 10
  • 28
0

There are different ways. Either you may use an index variable to access the field like mentioned by others here, or you may use a pointer. You could add an extra empty field in your array to mark it as end of data:

names my_data[] = {
    {"First", "Row",  20, 12},
    {"Second", "Row", 55, 30},
    {"Third",  "Row", 80, 47},
    {"Fourth", "Row", 27, 34}
    {NULL, NULL, 0, 0}
}; 

names* my_data_ptr = my_data;
while (my_data_ptr->first_name) {
    printf("%s\n",my_data_ptr->first_name);
    printf("%s\n",my_data_ptr->second_name);
    printf("%d\n",my_data_ptr->x_position);
    printf("%d\n",my_data_ptr->y_position);
    my_data_ptr++;
}

The advantage is that you don't have to know the size of the array in advance.

bkausbk
  • 2,740
  • 1
  • 36
  • 52