C++ Pointer to Integer with Increment Example

Ansif P I
1 min readSep 26


#include <iostream>

using namespace std;
int main()
int x = 7;
int* ptr = &x;
cout << *ptr << " ";
cout << *ptr;
return 0;

Answer: 7 8


  • In this C++ code, an integer variable x is declared and initialized with the value 7.
  • A pointer ptr is declared and initialized to store the address of x using the address-of operator (&).
  • cout << *ptr << " "; prints the value pointed to by ptr, which is the value of x, i.e., 7. So, the first output is "7 ".
  • (*ptr)++; increments the value stored at the memory location pointed to by ptr. In this case, it increases the value of x from 7 to 8.
  • cout << *ptr; prints the updated value pointed to by ptr, which is now 8. So, the final output is "8".

This code demonstrates how a pointer can be used to access and modify the value of a variable indirectly.



Ansif P I

Managing partner at Sevendyne.com. Expertise in App development, Team building etc