Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Understanding Pointers and Dereferencing in C++

0 votes
asked Jul 2, 2020 by klars89 (160 points)

I have the following code:

1 #include <iostream>

2 using namespace std;

3 int main()
4 {
5     int x = 7;        // integer variable x contains value 7
6     int* ip = &x;    // integer pointer variable ip points to address of variable x
7     int &y = x;      // integer value at address at variable y is assigned value stored in variable x
9     cout << "The value of x is: " << x << endl;                        // value held in location referenced by variable x
10   cout << "The value of &x is: " << &x << endl;                    // address of variable x
11   cout << "The value where *ip points is: " << *ip << endl;   // value held in location pointed                                                                                                at by pointer ip
12   cout << "The value of &ip is: " << &ip << endl; // dereferenced pointer?
13   cout << "The value of ip is: " << ip << endl;     // address of pointer ip
14   cout << "The value of y is: " << y << endl;       // value held in location referenced by                                                                                       variable y
15   cout << "The address where y is: " << &y << endl;    // address of variable y
17    return 0;
18  }

The comments are for my benefit to make sure I understand pointers, dereferencing, and the relationship between these and variables.

I understand pointers to basically be variables that only "contain"/point to memory addresses. However, line 12, the "dereferenced pointer" is throwing me off. It displays a memory address different from the others from lines 10, 13, and 15. The other lines all display 7 to the console, as I expected.  Does anyone have an explanation, and if so, is there an easy way to understand why this is occurring?

Thank you!

3 Answers

+1 vote
answered Jul 4, 2020 by Peter Minarik (89,040 points)
selected Aug 7, 2020 by klars89
Best answer

Address of Operator (&)

Think of the & operator as "address of".

int something = 42;
int * addressOfSomething = &something;

So addressOfSometing only stores the address of the variable something.

Dereferencing (*)

Dereferencing is not the above though! Dereferencing is when you look at an address (e.g. addressOfSomething) and try to access the value stored on that address. It's achieved via the * operator.

int valueOfSomething = *addressOfSomething;
// valueOfSomething now stores the same value as something


Keep in mind, that both the & and * operators mean different things in different contexts (e.g. they can be bitwise operators too or multiplication or indicating that a type is a pointer -- e.g. int * --).

0 votes
answered Jul 3, 2020 by Admin (5,130 points)
line 10: &x will be address of variable x
line 12: &ip will be address of variable ip, Its not dereferencing operation. *ip is called dereferencing and it should be equal to x, whereas ip would be equal to &x, since you have assigned ip = &x
line 13: ip, which should be equal to &x
line 15: &y, printing address of y, since y is alias of x, it should be same as &x
0 votes
answered Jul 4, 2020 by LiOS (6,420 points)
From StackOverflow - A dereferenced pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

In this case, the & is used to print out a memory address of where the variable is located in RAM.

Simply, line 12 is actually printing the address of variable ip, not the value stored (the address of x) or the value that it points to (the value of x).
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.