|
-
July 12th, 2002, 09:19 AM
#1
Simple struct/union/etc syntax question
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 [ccode] [/ccode] to format C/C++ code to be readable. Is there any comparable feature with the new forum software?
Last edited by Bob Davis; July 12th, 2002 at 09:22 AM.
-
July 12th, 2002, 09:27 AM
#2
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
C G C F A D--Feel the Noise
"When your life goes nowhere and leads back to me, doesn't that tell you something?"
~Gray Area Fury
-
July 12th, 2002, 09:39 AM
#3
Re: Simple struct/union/etc syntax question
Howdy Bob!
Does the code below answer your question?
Code:
typedef struct _Node
{
// ...
_Node* d;
} Node;
If not, please give an example.
Regards,
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
July 12th, 2002, 09:49 AM
#4
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
-
July 12th, 2002, 09:59 AM
#5
Of course, in C, zdf's sippet would have to be:
Code:
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 12th, 2002, 10:17 AM
#6
To answer your added question: use [ CODE ] and [ /CODE ], without the spaces.
Jeff
-
July 12th, 2002, 10:45 AM
#7
While I'm at it, I've got another stupid question. I know with the old-style forums, you could use [ccode] [/ccode] 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.
Ce n'est que pour vous dire ce que je vous dis.
-
July 15th, 2002, 03:30 AM
#8
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:
Code:
struct _data
{
// no reference to struct _data
};
typedef _data data;
Regards,
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
July 15th, 2002, 04:26 AM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|