1

これは非常に単純な問題に違いありません。4 つの要素を持つ構造体があり、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;
   }
4

4 に答える 4

5

ループで正しい:

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

:配列添字演算子の優先順位は、オブジェクト名演算子によるメンバー選択よりも高いため、括弧は必要ありません。[].()

また

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

次に、+プラス演算子は優先順位が低いため、優先順位を上書きするために括弧が必要です。

于 2013-08-20T07:51:20.907 に答える
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;
   }
于 2013-08-20T07:51:47.653 に答える
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[0] を指すポインターとして my_data を使用しています

于 2013-08-20T07:53:53.490 に答える