DSA-1.8 : Pointers

PHOTO EMBED

Mon Feb 27 2023 13:31:40 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

#include <iostream>
using namespace std;

int main()
{
  int A[5] = {10,20,30,40,50};
  int *p , *ptr;
  p = A; 
  ptr = &A[0];
/*
no need to give the ampersand(&) when we are giving array name to the pointer, because name of an array A itself is the starting address of this array. So, p is the pointer so, it can store the address.
If we want to use ampersand(&) then we should say A of zero, A[0] means this to 10, A[0] is 2 and its address, then you should write the address.
*/
  
for (int i=0; i<5; i++)
{
    cout << A[i] << endl;
    //To access the values using pointer instead of array name:
    // cout << p[i] << endl;
}
  
  return 0;
}
content_copyCOPY

Use of pointers for arrays and for printing the values in an array. NOTE : no need to give the ampersand(&) when we are giving array name to the pointer, because name of an array A itself is the starting address of this array. So, p is the pointer so, it can store the address. If we want to use ampersand(&) then we should say A of zero, A[0] means this to 10, A[0] is 2 and its address, then you should write the address.