1

I have the code:

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 !!!

}

The compiler throw the error:

  • "ptrInf" is not a member of InformationListStruct, because the type is not yet defined in function main()

If I put this line:

typedef struct InformationListStruct *PtrInformationListStruct;

after this lines:

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

then other error appears:

  • Type name expected in function main()
  • Declaration missing ; in function main()

How to get "ptrInf" correctly?

In silico
  • 51,091
  • 10
  • 150
  • 143
amigo
  • 343
  • 5
  • 19
  • 3
    Just a small remark: putting type definition in function body is not a really good pratice to me. – Synxis Jun 13 '12 at 09:05
  • Do you get other errors? When you have a question about errors, it's always best to add _all_ the errors to the question, and preferably verbatim (i.e. copy-paste). – Some programmer dude Jun 13 '12 at 09:07
  • I am using Unix GCC and I see below errors t.c: In function ‘main’: t.c:22:51: error: ‘InformationListStruct’ undeclared (first use in this function) t.c:22:51: note: each undeclared identifier is reported only once for each function it appears in – Viswesn Jun 13 '12 at 09:43

3 Answers3

5

You shouldn't cast malloc()'s return in C. Also, use sizeof, don't repeat the type name:

list = malloc(sizeof *list);
Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • @LuchianGrigore It can work around the error, your solution does the same by adding the missing `struct` keyword to make the type name legal. My view is that *having* the type name there is almost always a bad idea; the above works as long as the definition of `list` works which it did in the question. – unwind Jun 13 '12 at 09:32
  • still the same error. I tried the code in gcc (under Linux) and it compiled without errors. It seems, that my Borland C++ 5.5.1 for Win32 works bad. – amigo Jun 13 '12 at 09:47
3

You need

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

or

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

since you have a typedef for it.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Which Compiler are you using, in visual studio , your code is compiling successfully. Please avoid type definations inside the body of a function.

Tejendra
  • 1,874
  • 1
  • 20
  • 32