January 21st, 2003, 06:45 AM
References Vs Anonymous Unions
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
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.
Last edited by bsuribabu; January 21st, 2003 at 06:48 AM.
January 23rd, 2003, 06:38 AM
not correct 100%
I think, your assumption is not correct 100%:
1. here, there is 80% truth. yes, Reference is a kind of alias for another variable. Yes, youi can create a reference to a variable and use that reference in the same way. But, you have to remember, that you cannot create reference without creating and initializing variable first, otherwise you will get problem. More strcitly, reference is "syntactic sugar", it is a pointer which is always dereferenced for you.
and you cannot create 2 two variables sharing the same memory
with reference. you just create another names for that variable. but that names are pointers and their value is the address of original variable.
2. and it is union, which allows creating 2 variables "really" sharing the same memory. But only one can exist at the time.
Even if anonymous union contains two variables of same datatype
like, does not make sense, because when you will have 1 int space in memory.
The main reason to have union is to safe memory.
in this and it will not act in the same way as reference does. for example: