Can anyone explain why output is garbage for char in a union in this code ?

S

Sid

Hi folks,

I wrote this code to test if I get the same address for all the
variables in a union but for some reason the address I got when I used
a char variable in a union seems bizarre- can anyone help explain why
I get a bizzarre address for a char variable inside a union ? In other
words why is &(x.i) not 0012FED0 in the code below ?



Below is the source code

#include <iostream>
using namespace std;


union Packed { // Declaration similar to a class
char i;
short j;
int k;
long l;
float f;
double d;
// The union will be the size of a
// double, since that's the largest element
}; // Semicolon ends a union, like a struct

int main() {
cout << "sizeof(Packed) = "
<< sizeof(Packed) << endl;
Packed x;
cout <<" &x=" << &x << endl ;
x.i = 'c';
cout << x.i << endl;
cout << "&(x.i)=" << &(x.i) << endl;

x.j=32;
cout <<x.j<< endl;
cout << "&(x.j)="<< &(x.j) << endl;

x.k =58;
cout <<x.k << endl;
cout <<"&(x.k)="<<&(x.k) << endl;

x.l=1000000;
cout << x.l << endl;
cout <<"&(x.l)=" << &(x.l) << endl;

x.d = 3.14159;
cout << x.d << endl;
cout <<"&(x.d)=" << &(x.d) << endl;

x.f=3.14f;
cout << x.f << endl;
cout <<"&(x.f)=" << &(x.f) << endl;
} ///:~

OUTPUT OF THE ABOVE CODE BELOW

sizeof(Packed) = 8
&x=0012FED0
c
&(x.i)=c╠╠╠╠╠╠╠╠╠╠╠└
↕ ???????????????????????????????????????
32
&(x.j)=0012FED0
58
&(x.k)=0012FED0
1000000
&(x.l)=0012FED0
3.14159
&(x.d)=0012FED0
3.14
&(x.f)=0012FED0
 
L

lallous

Sid said:
Hi folks,

I wrote this code to test if I get the same address for all the
variables in a union but for some reason the address I got when I used
a char variable in a union seems bizarre- can anyone help explain why
I get a bizzarre address for a char variable inside a union ? In other
words why is &(x.i) not 0012FED0 in the code below ?



Below is the source code

#include <iostream>
using namespace std;


union Packed { // Declaration similar to a class
char i;
short j;
int k;
long l;
float f;
double d;
// The union will be the size of a
// double, since that's the largest element
}; // Semicolon ends a union, like a struct

int main() {
cout << "sizeof(Packed) = "
<< sizeof(Packed) << endl;
Packed x;
cout <<" &x=" << &x << endl ;
x.i = 'c';
cout << x.i << endl;
cout << "&(x.i)=" << &(x.i) << endl;

x.j=32;
cout <<x.j<< endl;
cout << "&(x.j)="<< &(x.j) << endl;

x.k =58;
cout <<x.k << endl;
cout <<"&(x.k)="<<&(x.k) << endl;

x.l=1000000;
cout << x.l << endl;
cout <<"&(x.l)=" << &(x.l) << endl;

x.d = 3.14159;
cout << x.d << endl;
cout <<"&(x.d)=" << &(x.d) << endl;

x.f=3.14f;
cout << x.f << endl;
cout <<"&(x.f)=" << &(x.f) << endl;
} ///:~

OUTPUT OF THE ABOVE CODE BELOW

sizeof(Packed) = 8
&x=0012FED0
c
&(x.i)=c╠╠╠╠╠╠╠╠╠&#956
8;╠└
↕ ???????????????????????????????????????
32
&(x.j)=0012FED0
58
&(x.k)=0012FED0
1000000
&(x.l)=0012FED0
3.14159
&(x.d)=0012FED0
3.14
&(x.f)=0012FED0

Maybe the &(x.i) was considered as a (char *) ...
Try to cout as: cout << (void *)&(x.i)
 
M

Mike Wahler

Sid said:
Hi folks,

I wrote this code to test if I get the same address for all the
variables in a union but for some reason the address I got when I used
a char variable in a union seems bizarre- can anyone help explain why
I get a bizzarre address for a char variable inside a union ? In other
words why is &(x.i) not 0012FED0 in the code below ?

In your statement:

cout << "&(x.i)=" << &(x.i) << endl;

... the expression '&(x.i)' has type 'char *',
which 'cout <<' will interpret as a pointer to
a string, so it will print all the characters
beginning at that address, up to one with a value
of zero (note that the first character output is
a 'c', the value you assigned to 'x.i'). You can
get it to print the address of 'x.i' if you cast
to another pointer type, e.g. 'void*'.

cout << (void *) &x.i << endl;

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top