-1
typedef struct person *person_t;

In this case, what is person_t exactly? is it a pointer to person or person? In C++, if I were to declare a ptr to an object, I would do:

person* p;

In this case, does it mean I can just write:

person_t p?

Thanks.

ohshire
  • 1
  • 1
  • 1
    That's a poor `typedef`. You should use `typedef struct person person_t;`. For a pointer type, you should use something along the lines of `typedef struct person* person_ptr;`. – R Sahu Jan 08 '15 at 05:37

3 Answers3

2

person_t is struct person *

So you can just have

person_t p;

When you do

typedef struct person *person_t;
person_t p;

p is a pointer to structure struct person

Else if you have

typedef struct person person_t;

Then

person_t *p;

will give you a pointer to your structure which is p

PS: I would always go by the latter one which I feel is more readable and less confusing. (Purely IMO)

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

It is act like pointer to a structure.

 struct person *p; // is equivalent to person_t p;

Because you are type casting the pointer also.

 person_t p; // it  will act like a pointer to  structure.

You can refer this link.

Community
  • 1
  • 1
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
0

This declaration

typedef struct person *person_t;

introduces alias person_t for type struct person *. So you can use either person_t or struct person * interchangeably. For example these two declarations are equivalent

struct person *p1;
person_t *p2;

The problem is that using identifier person_t for denoting pointer of type struct person * is not a good choice. The reader of the code can think that person_t is an alias for the structure person itself. It would be better for example to declare the alias of the pointer like person_ptr or something else that it would be seen that this type denotes a pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Wait, the part that confuses me the most is whether to use person_t *p2, or person_t p2, do we still keep the star? – ohshire Jan 09 '15 at 06:10
  • @ohshire You defined in the typedef person_t like struct person *. So they are interchangeable. If you will write person_t * then it will be equivalent to struct person ** that is it will be pointer to pointer. – Vlad from Moscow Jan 09 '15 at 06:14