Sign in to follow this  

Things I still dont really get

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well ive been at this for 1-2 years and when I look at some of the code from widely used headers (windows.h) (string,vector ect) there are a few things that pop out at me as "that really weird syntax" here it is
union
{
   X
   Y
   Z
}

ok so WHAT IS a union and what advantage does it have over things like structs
and classes I didn't find any good articles on this site about unions and no good explenation in any of the books I have.

another is an implementation of typedefs that ive seen

typedef struct NAME{
    X
    Y
    Z
} NAME, *NAME;

what does this mean? in the perticular refrence i thinking of the second Name 
at the bottom is actually a pointer but im not sure if thats relevent

how is this diffrent from 
struct Name
{
  X
  Y
  Z
}
I guess the sorce of most of my confusion is from the weird combinations of the key works 'typedef, struct and union' any clarification, and if this topic is just too broad feel free to point me to a link thanks [smile] very strange

Share this post


Link to post
Share on other sites
1. A union stores multiple values (usually of different types) in the same memory address. (Actually, I don't know what the standard says about where the data is stored, but the basic idea is that you only use as much memory as is required for the 'largest' type in the union). Here is the first hit from a google for 'c++ unions'.

2. Aside from a couple of syntax errors, the first struct example you have there is C-style (I'm pretty sure) and typedef's both the struct itself and a pointer to the struct, while the second example is how you would declare a struct in C++.

Share this post


Link to post
Share on other sites
Quote:

what advantage does it have over things like structs


One "advantage" is the ability to use more than one way to access contained data:

union my_vector
{
struct
{
int x, y, z;
} point;
int vector[3];
};

...

my_vector v;

// Showing two ways to access the same variable
v.point.x = 0;
v.vector[0] = 0;
...


When implementing the [] operators of a class, the vector is more handy, while accessing the components directly, the point representation is more handy.
Just a matter of taste I guess.
The first time I saw this way of keeping local data was in the source code of Descent 1.

I have also seen this type of construct used in the implementation of a simple script language. Im sure you have seen script languages where a variable can contain "anyhing". Integer, float, string. Or pointer to function (used for so called built-ins) and so on. My guess is they did so to preserve memory space.
However, I suspect implementing a script language that way is "old-style" these days.

I cant think of any other/better uses right now...

Share this post


Link to post
Share on other sites
Re: the second issue.

In C, structures don't become first-class citizens of the type world. If you create a structure MyStruct, and want to instantiate it, you have to write "struct MyStruct MyVariable;" because without that "struct" keyword "MyStruct" doesn't mean anything to the compiler.

That's not terribly nice to read, so the typedef assigns a first-class name to the structure. If you change the structure's name to "_tagMYSTRUCT" but typedef it to "MyStruct," you can then do the C++-style thing of just writing "MyStruct MyVariable."

Share this post


Link to post
Share on other sites
Quote:
typedef struct NAME{
X
Y
Z
} NAME, *LPNAME;

This syntax is a type definition of 3 types: struct NAME, NAME, and LPNAME. 'struct NAME' and 'NAME' are identical. 'LPNAME' is defined as a pointer to 'NAME'.

You could expand the syntax like this:
struct NAME {
X
Y
Z
};
typedef struct NAME NAME;
typedef NAME *LPNAME;

Share this post


Link to post
Share on other sites

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this