简体   繁体   English

如何访问数组中结构的成员?

[英]How to access members of a structure in an array?

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;
   }

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. 第二, +加号运算符具有较低的优先级,因此我们需要括号来覆盖优先级。

You need to put the i :- 你需要把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;
   }
//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] 在您的示例中,您使用my_data作为指向my_data [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. 优点是您不必事先知道数组的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM