DSA 1.8 : Pointers


Mon Feb 27 2023 13:46:58 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

#include <iostream>
using namespace std;

int main()
    int *p;
    p= new int[5];  
    //will allocate memory for 5 integers, so it is an array of integers and it is assigned to p
    //initializing the values
    p[0] = 10;
    p[1] = 20;
    p[2] = 30;
    p[3] = 40;
    p[4] = 50;

    for(int i=0; i<5; i++)
      cout << p[i] << endl;
  	delete [] p;   //to delete an array, first use square brackets and the the name of the variable 		to be deleted
  return 0;

Creating an array in heap memory using C++ programming language.