typedef struct identifiers

  • Thread starter godfatherofsoul
  • Start date
G

godfatherofsoul

I notice that sometimes typedef struct definitions have an additional
"prefix" identifier like so:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

What is the rationale for the extra name and what are the implications
of leaving it off?
 
J

Jonathan Mcdougall

I notice that sometimes typedef struct definitions have an additional
"prefix" identifier like so:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

What is the rationale for the extra name and what are the implications
of leaving it off?

This is a relic from C, where structs had to be explicitly qualified
when instantiated:

struct A {};

// A a; <-- illegal
struct A a; // ok

A workaround was to typedef the struct. This is not necessary in C++.
Don't do it.


Jonathan
 
M

Marcus Kwok

Jonathan Mcdougall said:
This is a relic from C, where structs had to be explicitly qualified
when instantiated:

struct A {};

// A a; <-- illegal
struct A a; // ok

A workaround was to typedef the struct. This is not necessary in C++.
Don't do it.

I agree with everything Jonathan said, however I will provide a little
more information.

When you do:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

This creates a new name (Name_t) for the type "struct prefixName". If
you leave off "prefixName", then it creates a new name (Name_t) for the
unnamed struct with two char members.
 
T

Tomás

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

This creates a new name (Name_t) for the type "struct prefixName". If
you leave off "prefixName", then it creates a new name (Name_t) for the
unnamed struct with two char members.

Slightly off-topic... but I have a question.

If you do the following in C:

struct Monkey { int i; };

Then you have to declare objects of it like so:

struct Monkey object;


But if you do the following:

typedef struct Monkey { int i; } Ape;


Then can you declare an object of it as follows:

struct Ape object; //using "struct" prefix

Or can you only do:

Ape object;

I presume that you'd still be able to write:

struct Monkey object;

but NOT:

Monkey object;


-Tomás
 
J

Jonathan Mcdougall

Tomás said:
Slightly off-topic...

Not at all.
but I have a question.

If you do the following in C:

struct Monkey { int i; };

Then you have to declare objects of it like so:

struct Monkey object;
Yes.

But if you do the following:

typedef struct Monkey { int i; } Ape;

Then can you declare an object of it as follows:

struct Ape object; //using "struct" prefix

No, you cannot. That's the beauty of a typedef: you don't care what's
the underlying type. It is not necessary to put "struct" before it and
what's not necessary is mostly illegal in C and C++.
Or can you only do:
Ape object;
Yes.

I presume that you'd still be able to write:
struct Monkey object;
Yes.

but NOT:
Monkey object;

That's right.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,439
Messages
2,571,700
Members
48,796
Latest member
Greg L.
Top