•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
> (*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.
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.
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.
•
•
Join Date: Oct 2007
Posts: 45
Reputation:
Rep Power: 1
Solved Threads: 1
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- C command-line I/O question (C++)
- Apache Alias Directive... mod_alias question (Linux Servers and Apache)
- Completely new to C++ and have question about using char (C++)
- Question (Geeks' Lounge)
- question on cooling (Cases, Fans and Power Supplies)
- Context-sensitive grammar question :( (Computer Science and Software Design)
- Welcome PC Mod Kingdom peeps! (Geeks' Lounge)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
- Changing Network Configuration (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: How to configure Windows Installer...
- Next Thread: Need help adding a search function.



Linear Mode