473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

typedef struct identifiers

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?

May 5 '06 #1
4 2180
go*************@yahoo.com wrote:
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

May 5 '06 #2
Jonathan Mcdougall <jo***************@gmail.com> wrote:
go*************@yahoo.com wrote:
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.


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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 5 '06 #3
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
May 6 '06 #4
Tomás wrote:
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...


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

May 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gurikar | last post by:
Does typedef vector<std::string> vNames; creates vector object?
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
4
by: James Brown | last post by:
I can't find this covered in the FAQ: typedef int foo; typedef struct foo { int a, b, c; } bar;
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
5
by: Alan Woodland | last post by:
Hi, I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
8
by: Guillaume Dargaud | last post by:
I just saw the following two lines in /usr/include/usb.h and my head is spinning: struct usb_dev_handle; typedef struct usb_dev_handle usb_dev_handle; What is that supposed to mean ? I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.