C Language



Admission Enquiry Form

  

Pointer to Array in C

What is Pointer to Array in C?

Pointer is a special varible that stores the address of another variable but of same type.It not only hold the address of one varible but also the address of elements of an array.
Consider the following Example:

Example: Pointer and Array

Output

&marks[0]= 2358832
&marks[1]= 2358836
&marks[2]= 2358840
&marks[3]= 2358844
&marks[4]= 2358848
Address of array marks is: 2358832



In the above output we can see that there is a diffrence of 4 bytes between two consecutive elements of array marks.This is only due to the size of int is 4 byte in the compiler.

We can notice that address of marks and &marks[0] is the same.The reason is that variable marks points to the first element of an array.

From the above example, we can say that marks is equivalent to &marks[0] and *marks is equivalent to marks[0]
Similarly,

  • marks+1 is equivalent to &marks[1] and *(marks+1) is equivalent to marks[1]
  • marks+2 is equivalent to &marks[2] and *(marks+2) is equivalent to marks[2]
  • ---------
  • marks+i is equivalent to &marks[i] and *(marks+i) is equivalent to marks[i]




One Way: Pointer and Array

Output

Enter marks
70
89
77
90
86

Marks are : 70
Marks are : 89
Marks are : 77
Marks are : 90
Marks are : 86


Explanation:

In the above example ,marks is holding the base address that is assigned to ptr. So, that to get the next address we have to do ptr++, which will point to next location.



Another Way: Pointer and Array

Output

Enter marks
10
20
30
40
50

Marks are : 10
Marks are : 20
Marks are : 30
Marks are : 40
Marks are : 50

Explanation:

In the above program,we are assigning the address of marks inside the loop using &marks[i] to the ptr.So, there is no need to do ptr++.Because as the value of i increases &marks[i] points to next location of an array.