Understanding Memory Allocation and Pointers in C++: A Beginners Guide

Written by: Manish Methani

October 20, 2021 at 2:04pm IST. 4 min 29 sec
Pointers in C++

What happens in memory when we write statement like this ?

int i = 3 ;

Compiler will tell the memory to store value 3 at some location and name that location as 'i' .It looks like this ,

Diagram itself is explanatory. The value of 'i' is 3 and it is stored at some location Number . You don't have to worry about the location Number . Memory will automatically handle this.

#include <iostream>
using namespace std;
int main()
{
  int a = 5;
  cout <<"Address of variable a = "<< &a <<endl;

  cout<<"Value of variable a = "<<a <<endl ;


  cout<<"Value of variable a = "<< *(&a);
  return 0;
}

Output

Address of variable a = 12345678
Value of variable a = 5
Value of variable a = 5

* asterik indicates that the variable is pointer variable. The third cout statement

 cout<<"Value of variable a = "<< *(&a);

Here , first we get the address of variable 'a' using &a and apply that address * to get the value at that specific address.

What are Pointers ?

Pointers are used to get the value at specific address Simple and Easy Defination. Pointers are denoted by *(Asterik symbol).

Syntax

datatype *variableName;

Example

int *a;
float *b;
float **b;
float ***b;

Pointers Concept Example

int i=3;                                              
j = &i;  

Always read * as "Value at"

 Assume 1000 & 1004 are the addresses of i & j
   i 
 1000 --> 3  
                                                                     
   j
1004 --> 1000    
                            
            
*j -->  Read Value at j 
        Value at 1000
        What is the value at 1000 Address ? :) 

*j --> 3                    
   

/* Yes you have to read the pointers like this. */

Example :-

#include <iostream>
using namespace std;
int main()
{
    int i=3, *j, k;
    j = &i;
    cout<< i * *j * i + *j;
    return 0;
}

Output :-

30

Compute the value of j like i explained in above example.

i = 3

*j = 3

i * *j * i + *j 
3 * 3 * 3 + 3  = 30

Always remember , Practice makes you perfect in Pointers :)

FAQ

1. What happens when we write `int i = 3;` in C++?

This statement allocates memory to store the value 3 at a certain location named "i".

2. How are pointers represented in C++?

Pointers are denoted by the asterisk symbol `*`. They are variables used to access values at specific addresses in memory.

3. What is the purpose of the asterisk symbol (*) in C++ pointers?

The asterisk symbol `*` is used to declare pointer variables and to access the value at a specific memory address.

4. How do pointers work in C++?

Pointers hold the memory address of another variable. By using the `*` symbol, they can access and manipulate the value stored at that address.

5. What is the significance of the `&` operator in C++?

The `&` operator is used to get the memory address of a variable in C++.

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com