473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why does 'a' change?

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
5 1309
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
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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Louise | last post by:
Hi I have written an HTML pages which does not have any colour specifying tags as far I know. When I view this in an Microsoft internet explorer browser it appears with a white background and...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
11
by: JS | last post by:
Dear Group, I have a webpage of small images. At the bottom is a Javascript function. If someone scrolls impatiently to the function link, when the action is complete the html does not continue...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
2
by: Mikus Sleiners | last post by:
I have a control - textBox1 that is binded to objects propery - "Currency" and another control - textBox2 (read only) that is also binded to same propery. Now, i have a situation where textbox1...
0
by: rn5a | last post by:
A custom server control containing a Button inherits from the WebControl class. I want to give users the option to change the BackColor of the Button. If this is the code in the ASPX page that uses...
17
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.