Pointer

closed account (oj2wbRfi)
what the difference ? how can we read this two lines.

1
2
3
4
5
6
  
 iPoint = &number;

*iPoint = 40;

number=40;


As in
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   int number = 10;
   cout << number << '\n';      // 10

   int *iPoint;                 // declares a pointer to an integer
   iPoint = &number;            // point to the memory address where number is stored
   *iPoint = 40;                // change the value at that address
   cout << number << '\n';      // so, magically, number is now 40 
}
Last edited on
L2 iPoint is set to the memory address of the variable number. In this context, & means 'Address of'.

L4 the memory pointed to by iPoint is referenced and the memory contents set to the value 40. In this context * means 'pointer de-reference'.
Topic archived. No new replies allowed.