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. 5 1326 yu****@gmail.co m 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_Keit h) 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. yu****@gmail.co m 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> yu****@gmail.co m 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 yu****@gmail.co m 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
spi...@gmail.co m wrote: yu****@gmail.co m 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 black text but
when I change
Windows start menu->settings->control panel ->display -> appearance
and change scheme to 'High Contrast Black' the background in the
browser changes to black and the text to white.
I understand that the windows scheme...
|
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 says:
"The 'visibility' property takes the value 'collapse' for row, row group,
column, and column group elements. This value causes the entire row or
column to be removed from the display, and the space normally taken up by
the row or column to...
|
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 loading the images.
How is this situation best dealt with?
Thank you for any assistance you can offer.
JS
|
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 works.
Here's the setup. I have a function, called GetEmployeeCertifications,
that is going to make a call to a SQL DB and return a result set. This
module calls another function, called FillParameters, to determine if
SQL parameters need to...
|
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 can be done using the designer
but I intentionally don't want to use that. The one reason is that you
cannot change the code generated by the designer. The other could be that
you have more free hand and control to design your GUI.
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 control does NOT refresh it's display
according to value set to underlying propery while at the same time
textBox2 does refresh accordingly.
Both textboxes ar located on the same form and there are no diferences in
their bindings.
|
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
the custom server control (whose class name is 'MyCls') which has been
compiled into 'MyDLL.dll' using VBC
<%@ Register Assembly="MyDLL" Namespace="MyNS" TagPrefix="cc1" %>
<form runat="server">
<cc1:MyCls ID="cccls" BackColor="red"...
|
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 same problem seems to occur.
Here the deal : when I run this piece of code, I expect all the memory
allocated by the "Test" object to be freed but what I observe is that
after the second sleep (after all the additions to the vector), the
memory...
|
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 is what the ListBox uses for its display. Correct?
|
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 uiform As Form1, ByRef si As
MTCC02.Form1.SiteRunOpts, _
ByVal numitems As Integer)
#End Region
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |