473,511 Members | 16,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Jul 22 '05 #1
3 1543
"Sid" <si**********@yahoo.com> wrote in message
news:18**************************@posting.google.c om...
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)

--
Elias
Jul 22 '05 #2

"Sid" <si**********@yahoo.com> wrote in message
news:18**************************@posting.google.c om...
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
Jul 22 '05 #3
Sid
Thanks a bunch, Mike. I tried ur suggestion -casting to a void pointer
and it worked.

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<Vw*****************@newsread1.news.pas.earth link.net>...
"Sid" <si**********@yahoo.com> wrote in message
news:18**************************@posting.google.c om...
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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1425
by: Claudio | last post by:
     printf ("\n %u " , sizeof ( ub ) ) ;      printf ("\n %u " , sizeof ( sb ) ) ;      printf ("\n %u " , sizeof ( n ) ) ;...
22
2076
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
142
6695
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory...
43
3832
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize =...
18
1918
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
30
3227
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
10
1467
by: Ajinkya | last post by:
int main() { struct num { int x,y; } val = {1,1,2,3,4,5,6,7}; struct num *ptr = val; int i=0; for(;i<4;i++) { ptr->x = ptr->y, ++ptr++->y; printf("%d,%d ", val.x, val.y);
2
1211
by: puzzlecracker | last post by:
Why Attempt #3 is Deplorable First, let's consider a few reasons why Attempt #3 above truly is deplorable form: 1. Alignment. Unlike dynamic memory acquired by ::operator new(), the x_...
0
2763
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.