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

Article by: Manish Methani

Last Updated: 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++.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>