By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 2,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

why does 'a' change?

P: n/a
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.

Jul 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
yu****@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 11 '06 #2

P: n/a
yu****@gmail.com wrote:
>
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:Th*************@gmail.com>

Jul 11 '06 #3

P: n/a

yu****@gmail.com wrote:
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

Jul 11 '06 #4

P: n/a
yu****@gmail.com wrote:
Hi,

For the following program:
<snip>
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 11 '06 #5

P: n/a

spi...@gmail.com wrote:
yu****@gmail.com wrote:
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.
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.

Jul 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.