User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,013 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,614 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 168 | Replies: 5
Reply
Join Date: Jul 2008
Posts: 4
Reputation: sahmedsa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sahmedsa sahmedsa is offline Offline
Newbie Poster

Need Help with a question

  #1  
Jul 24th, 2008
int a = 10;
int* b = &a;
int c = 4;
int* d = &c;
(*d)++;
d = b;
*d = c - *b;
cout << a << " " << c;

Output
5 -5

I am having difficulties understanding pointers, like I know that *something means the value its pointing at, and &something means its the address, but is it possible for someone to walk me through this question as I believe it will clear all my doubts and help me understand better.
Thanks in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,638
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 416
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Need Help with a question

  #2  
Jul 24th, 2008
> (*d)++;
This means increment whatever d is pointing to.
Since it seems to be pointing at c, it's going to increment c.

> cout << a << " " << c;
There's nothing stopping you pasting this statement between every other line to see how things change.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: sahmedsa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sahmedsa sahmedsa is offline Offline
Newbie Poster

Re: Need Help with a question

  #3  
Jul 24th, 2008
ok so basically it means that
*b=&a
b=10
?
Reply With Quote  
Join Date: Oct 2007
Posts: 232
Reputation: joshmo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Need Help with a question

  #4  
Jul 24th, 2008
Originally Posted by sahmedsa View Post
ok so basically it means that
*b=&a
b=10
?

I suppose
Reply With Quote  
Join Date: Oct 2007
Posts: 45
Reputation: ff4930 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
ff4930 ff4930 is offline Offline
Light Poster

Re: Need Help with a question

  #5  
Jul 24th, 2008
int a = 10;
a is an integer with value 10

int* b = &a;
b is a pointer to an integer which points to the address of a

int c = 4;
c is an integer with value 4

int* d = &c;
d is a pointer to an integer which points to the address of c

(*d)++;
d is dereferenced which has the value of c which is 4 and ++ will make it value of 5

if a pointer is pointing to an address putting * to a pointer will "dereference" it meaning getting the value.

d = b;
d is equal to b meaning d points to the same address as b

*d = c - *b;
d derefrenced value is equal to c = 5 - derefrenced b = 10 = -5

remember d is pointing to same address as b which points to a so whatever changed to d
will have effect on a so thats why cout << a has -5

and c is = 5

cout << a << " " << c;

Output
5 -5
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: sahmedsa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sahmedsa sahmedsa is offline Offline
Newbie Poster

Re: Need Help with a question

  #6  
Jul 24th, 2008
O ok, that makes sense. Thank you for your help, really appreciate it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC