1

私はコードを持っています:

main() 
{
   typedef struct
   {
      int data;
   } Information;

   typedef Information *PtrInformation;

   typedef struct InformationListStruct *PtrInformationListStruct;

   typedef struct InformationListStruct
   {
      PtrInformationListStruct ptrNext;
      PtrInformation ptrInf;
   } PtrInformationListStructElement;

   //==============================

   PtrInformationListStruct list;
   list = (PtrInformationListStruct)malloc(sizeof(InformationListStruct));
   PtrInformation ptr = (*list).ptrInf;  // error !!!

}

コンパイラはエラーをスローします:

  • 「ptrInf」はInformationListStructのメンバーではありません。これは、タイプが関数main()でまだ定義されていないためです。

私がこの行を置くと:

typedef struct InformationListStruct *PtrInformationListStruct;

この行の後:

   typedef struct InformationListStruct
   {
      PtrInformationListStruct ptrNext;
      PtrInformation ptrInf;
   } PtrInformationListStructElement;

その後、他のエラーが表示されます:

  • 関数main()で期待されるタイプ名
  • 宣言がありません; 関数main()で

「ptrInf」を正しく取得するにはどうすればよいですか?

4

3 に答える 3

5

malloc()C で の return をキャストしないでください。また、 を使用sizeofし、型名を繰り返さないでください。

list = malloc(sizeof *list);
于 2012-06-13T09:22:53.017 に答える
3

あなたが必要

 list = (PtrInformationListStruct)malloc(sizeof(struct InformationListStruct));
 //                                               |
 //                                          note struct keyword

また

 list = (PtrInformationListStruct)malloc(sizeof(PtrInformationListStructElement));

あなたはそれを持っているtypedefので。

于 2012-06-13T09:03:40.937 に答える
1

Visual Studio で使用しているコンパイラはどれですか。コードは正常にコンパイルされています。関数の本体内での型定義は避けてください。

于 2012-06-13T09:05:58.330 に答える