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[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]