Pointers and Array

  • Arrays are internally stored as Pointers.
  • So , array element can be handled using pointer.
  • Any operation in Array which can be done by the subscripting(index number) that same operations can be done by pointers
  • till now we have seen array of int , array of float , array of characters,but now we will see the Concept of Array of Pointers

For understanding this concept will declare an array of 5 integer number
lets declare it:
#include<stdio.h>
void main()
{

int a[5] = {1,2,3,4,5};

}

Now will make an array pointers which will store the addresses of elements of array which we have declared above

Syntax:Declaring Array of Pointer.
Datatype    *pointer_array_name[no_of _elements];

Declaring and initializing:Array of pointers with size = 5.
#include<stdio.h>
void main()
{
int a[5] = {1,2,3,4,5};


int *p[5] = {&a[0],&a[1],&a[2],&a[3],&a[4]};

}

Let's Understand it visually.



Array 'p' is storing the addresses of elements of array 'a' , as elements in it.

  • Same initialization can be done with another way.
  • Let's see that way.
  • As you know writing &array_name[0] is equivalent of writing array_name
  • So we will use it.

Declaration and Initialization:
#include<stdio.h>
void main()
{
int a[5] = {1,2,3,4,5};


int *p[5] = {a,a+1,a+2,a+3,a+4};

}

Let's see another example:Array of float type:
#include<stdio.h>
void main()
{
float a[5] = {1.5,2.6,3.2,4.4,5.6};


float *p[5] = {a,a+1,a+2,a+3,a+4};
}

  • This is how we can make array's of Pointers.





Further Topics:




Further Topics:


People Also Searched: