Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

References vs Anonymous Unions

Status
Not open for further replies.

bsuribabu

Programmer
Apr 4, 2002
25
IN
Hi,

Here is a some analysis i have made on References and Anonymous Unions.

Pls tell me that my assumption is correct or not?

1. Reference is a alias for another variable means i can create a reference to a variable where i can use the reference in the same way where i can use the variable . Means some what we are creating the two variables sharing the same memory.

2. If anonymous union contains two variables of same datatype
like

union {
int a,b;
};
this declaration gives us that a,b sharing the same memory.
means if we feel "a" as a actual variable and b as a reference for it , it acts in the same way as reference does.


Suri
 
Hi bsuribabu,

I think you mixed C++ and C. In C there is nothing like a reference. Your "assumptions" seem right for C++. Note: you can think of an reference as if it where a shorthand for a pointer variable, because this is the way it is implemented. If you call a function with a reference paremeter you do not pass the variables value but a pointer to it. If you access the reference from within the called funktion you accually use the pointer and dereference it.
(Of course, references are not restricted to function parameters).

Now to unions. You are partly right in that unsion member share the same memory space. But, those members are by no means references to each other. This is because each member can have another type, like in structures. E.g. you can define the following union:
union {
int a;
float b;
struct {
double a;
char *s;
} s;
};
 
Hi fro,

I have wrongly posted this thread in C Language lounge.

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top