2

I've read in different posts that the implementation of struct can be hidden in C by doing the following:

test.h:

typedef struct SomeTest *SomeTest;
SomeTest make();

test.c:

#include "test.h"
typedef struct SomeTest {
    int data;
}*SomeTest;

What I wonder though: Is the line:

typedef struct SomeTest *SomeTest;

really needed that way? Wouldn't it be sufficient to:

test.h:

   typedef struct SomeTest SomeTest;
   SomeTest *make();

test.c:

#include "test.h"
struct SomeTest {
    int data;
};

It's just that the line:

typedef struct SomeTest *SomeTest;

gives me headaches sometimes,...and it hides the fact that "SomeTest" is a pointer...

Machavity
  • 30,841
  • 27
  • 92
  • 100
inzanez
  • 366
  • 1
  • 16

1 Answers1

10

It's just a matter of coding style. Many people, me including, are really confused when a pointer is hidden behind a typedef. Adding the little * at each occurrence is not really a big deal, but helps a lot to clarify things for the occasional reader.

The other aspect of that typedef is really bad: using the same identifier for things (the struct tag and the typedefed name) that are of different type (one a struct and the other a pointer) should not be allowed and really confuses. This is really bad, bad style and should never pass any code review.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177