3

I'm a beginner in C and I came across some codes in a header file that I'm not sure what style it uses and what it means.

typedef struct tActiveObject *testActiveObjectPtr; typedef struct tActiveObject { ActiveObject ao; int state; } testActiveObject, *testActiveObjectPtr; 

Why do we need to create a pointer as an alias i.e. testActiveObject and *testActiveObjectPtr? And is this just some C style that I am not aware of?

Thanks.

3
  • Possible duplicate of: stackoverflow.com/questions/1543713/… Please refer to tristopia's answer, I hope it explains what you need to know. Commented Mar 10, 2015 at 2:00
  • 1
    @PawełDuda Nah, not a duplicate, since that link doesn't explain the first typedef, or the struct tag etc. In fact this code doesn't make any sense at all, see my answer below. Commented Mar 10, 2015 at 7:42
  • @PawełDuda Thanks, but that answer only solve my question partially. Still helps me to understand the situation better though. Commented Mar 10, 2015 at 15:48

1 Answer 1

1

If the both those typedefs occur in the same header file, then the code doesn't make any sense. In that case, the first typedef is completely superfluous and the whole code could get replaced with

typedef struct { ActiveObject ao; int state; } testActiveObject, *testActiveObjectPtr; 

Otherwise if the typedefs were in different files, the code might have been a failed attempt to create a pointer to an incomplete type, but it doesn't look like that's the case. The struct tag is superfluous, but also smells like a failed attempt to create a self-referencing struct.

Also, good programming practice dictates that you never hide a pointer behind a typedef.

So it would seem the whole code was created by a rather confused person who didn't quite know what they were doing. If possible, throw the code away and replace it with:

typedef struct { ActiveObject ao; int state; } testActiveObject, ... testActiveObject* ptr; // whenever you need a pointer to this type 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.