2013-08-20 50 views
1

这必须是一个非常简单的问题,我有一个结构中有四个元素,一个结构变量被初始化为一个数组,现在问题是我可以访问数组的第一行,但我不知道如何访问剩余的行...请指导我!如何访问数组中的结构成员?

//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; 
    } 
+0

阅读:[结构指针](http://stackoverflow.com/questions/18254623/pointer-to-structure/18254652#18254652)我解释了如何使用指针结构也,我认为你的情况有帮助。 –

回答

5

在循环正确:

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

注意[]数组下标运算符的precedence是通过对象名称运营高于.成员选择,所以你不需要()括号。

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

第二,+再加上运营商具有较低的优先级,所以我们需要圆括号覆盖优先级。

1

你需要把i: -

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

更改代码: -

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

你应该索引阵列所需的项目;在你的例子中,你正在使用my_data作为指向my_data的指针[0]

0

有不同的方法。您可以使用索引变量来访问此处其他人提到的字段,也可以使用指针。您可以将您的阵列中添加额外的空场将其标记为数据结束:

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

的好处是,你不必知道数组的大小提前。