Pointers In arrays||Dynamic Allocation||Dynamic allocation of objects||Dynamic Allocation of variables||Dynamically Allocated Arrays

 

πŸ‘‰ POINTERS IN ARRAYS πŸ‘ˆ


-by Aaditya Nayak

Introduction:

As we all know pointer is a variable that points towards an address of a entity.

And you must have also seen the syntax to define a pointer variable pointing a single entity(variable or an object), which is :

data_type * pointer_name = variable_or_object_name;

Now in this blog we will see how to define a pointer pointing a group of elements or an array.

Syntax And Application:


Syntax:

data_type * pointer_name = array_name; (here, you don’t need to add &)

Explaination:

We don’t add & here because in an array say: arr – arr represents pointer towards first element which is arr[0] in arr.


Now, since arrays are contiguous blocks in memory lets consider a condition:

Condition: (arr is an integer array containing n+1 elements)
If first element arr[0] is present at an address x and size of integer in your system is 4bit so the next element will be present at address x+4 and so on, therefore nth element is present at x+(4*n).

Now, lets see how pointer works in the above condition, where pointer is defined as:

int * p = arr;

Now,

POINTER

POINTING TO

DEREFERENCED AS

p

arr[0]

*p

p+1

arr[1]

*(p+1)

p+2

arr[2]

*(p+2)

…………………………………

……………………………………

………………………………….

p+n

arr[n]

*(p+n)



Note: 

You can use arrow operator (->) same as in case of single object by using operator pointing towards a particular object in array of objects.

Dynamic Allocation of arrays:



By using pointers we can dynamically form arrays which means memory for the desired array is reserved in heap, which means we can also delete an element in an array anytime we want, which leads to more efficient usage of ram.

Syntax for dynamic allocation:

data_type * pointer_name = new data_type[number_of_elements] 
(where new is an operator)

Now lets see a code with dynamically allocated arrays.

Code:

#include <iostream>
#include <string>
using namespace std;

class demo
{
    int Employeeid;
    string Employeename;

public:
    void setemployee()
    {
        cout << "Enter Employee Name : ";
        cin >> Employeename;
        cout << "Enter Employee id : ";
        cin >> Employeeid;
        cout << endl;
    }
    void getemployee()
    {
        cout << "Employee name is : " << Employeename << endl;
        cout << "Employee id is : " << Employeeid << endl;
    }
};

int main()
{
    // Here making an dynamic array of 3 Employees

    demo *p = new demo[3];

    for (int i = 0i < 3i++)
    {
        cout << "For Employee number : " << i + 1 << endl;
        (p + i)->setemployee();
        (p + i)->getemployee();
        cout << endl;
    }

    // The below command will delete full array
    // delete[] p;
}

Output:




Now you are all done with basics of pointer in array. Now you can try monitoring ram usage using task manager in above program while objects are dynamically allocated and deleted and also while objects are statically allocated in the same program.


THANKS FOR READING THE BLOG 

I Hope This was helpful 😊









Comments

Popular posts from this blog

What Is Functions In C++ ? || Explained By Examples || Function Types

Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||

Reference and Pointers in C++