1

Possible Duplicates:
Why should we typedef a struct so often in C?
Difference between ‘struct’ and ‘typedef struct’ in C++?

What is the difference between the following type declarations?

struct Person
{
    int age;
};


typedef struct 
{
    int age;
}Person;

I understand that

struct 
{
    int age;
}Person;

Creates and instance of an unnamed struct called person, where

struct Person
{
    int age;
};

declares a type called person, but not an instance. But I still dont get what the typedef does.

Community
  • 1
  • 1
Anthony D
  • 10,877
  • 11
  • 46
  • 67

5 Answers5

5

I think that's the same as in C, typedef creates an alias of a type... in your first case, the name of the type is "struct Person", while in the second case is just "Person".

Usually, when you have to declare self referencing structures (like lists), you use both, because the typedef has not effect until the structure is defined (unless you make a forward declaration), for example:

typedef struct node {
    void *data;
    struct node *next;
} TNode, *PTNode;

so now you can declare variables of the same type in the following ways:

struct node *node1;
TNode *node2;
PTNode node3;

the three variables above are the same, pointers to the node structure.

fortran
  • 74,053
  • 25
  • 135
  • 175
  • This was the understanding I got from the wikipedia page. – Anthony D Jul 22 '09 at 17:11
  • In C++ (which is what the question was about), this is not the same as in C. – sbi Jul 22 '09 at 17:19
  • @sbi I cannot see how it differs from C to C++... maybe you'll be kind to explain it to us. – fortran Jul 22 '09 at 19:24
  • @fortran: See my answer. In C++, `struct X {}` can be referred to as `X` (while in C, it must be referred to as `struct X`). So in C++, there is no need for the `typedef` at all. Except when you write headers that are supposed to be parsed by a C compiler, too, this is just a C-ism that people use without thinking (or knowing) about. – sbi Jul 25 '09 at 19:32
2

In C, structs live in their own name space, so you have to write struct Person if you want to use the struct's type name. The typedef eliminates the need for that prefix.

In C++, structs live in the same name space as everything else, so there's no need to do this. It's usually seen as an unnecessary C-ism.

sbi
  • 219,715
  • 46
  • 258
  • 445
0

c-compatibility essentially same in c++, there for legacy c-code

not sure about referring to static functions and typedefs in the unnamed struct when you do the definition this way - I never do.

0

you can tell the difference when passing Person or struct Person to a function

If you defined

struct Person { ... };

You should pass it to a function explicitly stating that it is a struct

void foo(struct Person person) { ... }

On the other hand, if you defined

typedef struct Person { ... };

You must not state that it is a struct, since the keyword typedef allows you to actually define a data type, in this case Person and you pass it this way

void foo(Person person) { ... }

pretty much like any other data type

Hope this helps

nairdaen
  • 1,037
  • 2
  • 11
  • 19
  • You meant typedef struct { ... } Person; right? Anyway, this is only so in C, not in C++. – sbi Jul 22 '09 at 17:20
0

In C++ typedefs are synonyms for other types rather than distinct types. They are generally used for two purposes:

  • Creating portable code where you abstract the platform specific away. On one platform you can have typedef int int32 and on another platform typedef long int32. You would use int32 throughout your code and when porting to the a new platform you would only have to modify the typedefs to match your requirements.

  • Creating aliases for very complicated types. E.g. typedef char const* const* cppchar. Instead of writing char const* const* in your code you can now write cppchar.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256