why does 'a' change?

Y

yusufm

Hi,

For the following program:

char* ptr;
int a = 65;
ptr =(char*) &a;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";



*ptr = 66;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";



*ptr++;
*ptr = 67;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";

I get the following output:

ptr: 3221202256
*ptr: A
a 65
ptr: 3221202256
*ptr: B
a 66
ptr: 3221202257
*ptr: C
a 17218

I wanted to know, why does the value of a = 17218? Should it not be 67?

Thanks.
 
K

Keith Thompson

Hi,

For the following program:

char* ptr;
int a = 65;
ptr =(char*) &a;

What program? You've posted a code fragment, not a complete program.
This makes it difficult for us to try it ourselves.
cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";

And you've posted C++ code to comp.lang.c.

Please either re-write the code in C (use printf rather than
cout<<...) and re-post it here, or post in comp.lang.c++. In either
case, please post a complete compilable program.
 
F

Flash Gordon

Hi,

For the following program:

cout << "ptr: " << (unsigned int) ptr << "\n";

<snip>

For that program you probably want comp.lang.c++ which is down the hall,
past the water cooler, third door on the right. C and C++ are different
languages.
 
K

Kenneth Brody

Hi,

For the following program:

Well, ignoring that it's not a "program" but a "code fragment",
and ignoring the greater problem of it being C++ rather than C,
I shall answer it based on the C portions of the code.
char* ptr;
int a = 65;
ptr =(char*) &a; [...]
*ptr = 66; [...]
*ptr++;

This increments ptr.
*ptr = 67; [...]
I get the following output: [...]
ptr: 3221202256
*ptr: B
a 66
ptr: 3221202257

Note that ptr has been incremented.
*ptr: C
a 17218

I wanted to know, why does the value of a = 17218? Should it not be 67?

You have incremented ptr, and therefore are pointing to the byte
after the first byte of "a". Assuming that you are on a system
with sizeof(int) at least 2, and a little-endian CPU, what you
have done is stored 66+(256*67) into the memory occupied by "a".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

spibou

Hi,

For the following program:

char* ptr;
int a = 65;
ptr =(char*) &a;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";



*ptr = 66;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";



*ptr++;
*ptr = 67;



cout << "ptr: " << (unsigned int) ptr << "\n";
cout << "*ptr: " << *ptr << "\n";
cout << "a " << a << "\n";

I get the following output:

ptr: 3221202256
*ptr: A
a 65
ptr: 3221202256
*ptr: B
a 66
ptr: 3221202257
*ptr: C
a 17218

I wanted to know, why does the value of a = 17218? Should it not be 67?

Thanks.

Although your programme is a C++ one it's close enough to C
that I will give you an answer here.

You have declared a as int and ptr as pointer to char. This means
that if on your system int occupies a smaller number of bytes than
char, then when you write *ptr = some_value; not all of the bytes
corresponding to a get modified. So a doesn't get the value you want.
If you declare ptr as pointer to int or declare a as char then things
will
work as you expect.

In the future please post C++ questions in the appropriate group.

Spiros Bousbouras
 
S

spibou

Although your programme is a C++ one it's close enough to C
that I will give you an answer here.

You have declared a as int and ptr as pointer to char. This means
that if on your system int occupies a smaller number of bytes than
char, then when you write *ptr = some_value; not all of the bytes
corresponding to a get modified. So a doesn't get the value you want.
If you declare ptr as pointer to int or declare a as char then things
will work as you expect.

Come to think of it even in that case you will encounter problems
because you increment ptr. I'm not sure what you intended to achieve
by writing *ptr++ but I hope you realize that it is the same as writing
*(ptr++) ie it is ptr itself which gets incremented.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top