Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2013
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2013 by Ivor Horton; ISBN: 978-1-118-84571-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2013 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2015, 12:34 PM
Registered User
 
Join Date: Apr 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy What is wrong with this pointers coding

I just started reading POINTERS and made my first program in pointers but when I got output window It was really surprising for me to get an unexpected output for the first time,after that I ran example Ex4_06.cpp and when I edited some of It's parts then It also gave me some interesting results.
My program is given below and I just added my doubts in comments of doubted lines
Code:
 
#include<iostream>
using namespace std;
int main()
{
	int *pnum{};
	int num1{ 18 }, num2{ 117 };
	cout << num2<<endl;// here num2 is giving the expected value as of initialised. 
	pnum = &num1;
	num1 = *pnum + 18;
	cout << "number 1 is " << num1 << endl
		<< "it is at " 
		<< hex << pnum 
		<< " *pnum " << *pnum //here *pnum is giving output of 24 which has to be 18 
		<<endl<<num2;//as I didn't altered value of num2 by any mean so Supposed output has to be as per initialised value 117. 
	//but output which I'm getting is really strange it is 75. 
	pnum = 0;
	pnum = &num2;
	cout << endl << " *pnum " << *pnum;//here *pnum is giving 75 same as the value last num2 gave as in last cout. 
	num1 = *pnum * 10;//as in above line of code *pnum is giving 75 so supposed output is 75*10=750 but output I got is 492. 
	cout << endl << "Now number 1 is " << num1 << endl
		<< "It is at " << pnum << " and at that address *pnum is " << *pnum << endl;
	return 0;
}
OUTPUT which I'm getting is

117
number 1 is 36
it is at 0043FEB4 *pnum 24
75
*pnum 75
now number 1 is 492
It is at 0043FEA8 and at that address *pnum is 75
Press any other key to continue...


I don't know what is wrong Please help me to figure it out.
 
Old May 5th, 2015, 05:41 AM
Registered User
 
Join Date: May 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile pnum = 0;

pnum is a pointer,you can use *pnum = 0.

Last edited by kerncool; May 5th, 2015 at 06:20 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DAO Coding to ADO Coding udaybg1 VB How-To 0 August 9th, 2011 08:50 AM
Is what i'm coding wright or wrong falcon eyes C# 2008 aka C# 3.0 9 June 17th, 2010 08:36 AM
C/C++ Pointers reality_42000 C++ Programming 1 March 4th, 2007 01:26 AM
pointers Stuby085 Visual C++ 1 August 30th, 2003 11:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.