Click to See Complete Forum and Search --> : Simple struct/union/etc syntax question


Bob Davis
July 12th, 2002, 09:19 AM
This is probably a stupid question, but here goes. I've noticed in people's code that sometimes when they declare unions or structs, there is the string 'tag' as part of the name. I've always wondered, what does this mean, and what does it do? I've just never been introduced to this particular syntax. I always just use

typedef struct
{
int a;
char b;
double c;
} structName;



Am I missing something by using this syntax?

Edit: While I'm at it, I've got another stupid question. I know with the old-style forums, you could use to format C/C++ code to be readable. Is there any comparable feature with the new forum software?

Waldo2k2
July 12th, 2002, 09:27 AM
I don't believe your missing out, as far as i know it's pretty much the same as whether you use hungarian notation or not (btw i can't stand hungarian notation) and as far as ccode goes here you use (code) and (/code) replacing the ('s with ['s

//some code here

zdf
July 12th, 2002, 09:39 AM
Howdy Bob!

Does the code below answer your question?

typedef struct _Node
{
// ...
_Node* d;
} Node;


If not, please give an example.

Regards,

PaulWendt
July 12th, 2002, 09:49 AM
zdf's code snippet alludes to this, but I figure I'll express it more explicitly: if you have a structure and you want it to be self-referiential [ie: contain pointers to itself], you'll want to use the tag-notation Bob talked about earlier. A lot of people just get used to using that notation and do it by habit.

--Paul

Graham
July 12th, 2002, 09:59 AM
Of course, in C, zdf's sippet would have to be:

typedef struct _Node
{
// ...
struct _Node* d;
} Node;


but since both C and C++ standards reserve the use of leading underscores for compiler and standard library writers, we can't use "_Node", so many people would use "tagNode", since the standards (or at least the C standard does) refer to an identifier in that position as a tag.

jfaust
July 12th, 2002, 10:17 AM
To answer your added question: use [ CODE ] and [ /CODE ], without the spaces.

Jeff

Alexey B
July 12th, 2002, 10:45 AM
While I'm at it, I've got another stupid question. I know with the old-style forums, you could use to format C/C++ code to be readable. Is there any comparable feature with the new forum software?I type my code into VC, then use "Replace" to insert the [ color ] [ /color ] tags. Then, I copy-paste. It is a fairly fast way to make the code look good on this forum. Look at aslmost any of my past posts to see the results.

Also, there is [ php ] [ /php ] tag that colors the code automatically, but the colors are so horrible that I edited it out the first time I used it.

zdf
July 15th, 2002, 03:30 AM
Graham is right! All standard libraries reserve the leading underscore for their own use. This way the names in libraries are less likely to collide with user names.
We may find something like this:

struct _data
{
// no reference to struct _data
};
typedef _data data;

Regards,

stober
July 15th, 2002, 04:26 AM
Originally posted by zdf
Graham is right! All standard libraries reserve the leading underscore for their own use. This way the names in libraries are less likely to collide with user names.


But that rule is not enforced. You can legally use the understore if you want to. It can be a dangerous thing to do, but there is nothing that will prevent you from doing it.