1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

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++.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com