Pointers and Arrays

When an array is declared the compiler allocates the base address and sufficient amount of storage to contain all the element of arrays in contiguous memory locations. The base address is the location of the first element i.e. index 0 of the array.

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

The name x is defined as pointer pointing to first element x[0]. So value of x is 1000. The location/address where x[0] is stored is 1000

 x = &x[0] = 1000

  • If p is an integer pointer, p=x and p=&x[0] are equivalent. So you can easily see that *p and *(p+0) both refer to 1

Similarly x[1] and *(p+1)
x[2] and *(p+2) refer to the same elements in array ‘x’ .

  • The fact is what the ‘c’ compiler does internally. The compiler defines array name as the constant pointer to the first element when we say x[i], the ‘C’ compiler internally converts it to *(x+i)

This means all the following notations are same:

x[i] = *(x+i) = *(i+x) = i[x]

Subtraction of one pointer from another:
1.One pointer variable can be subtracted from another provided both variables point to elements of the same array. The resulting value is the number of elements separating corresponding array element.

Comparison of two pointer variables :
1.Pointer variables can be compared provided both variables point to the object of same data type.
2.Such comparisons are useful when both pointer variables point to the elements of the same array.


Mr. Sandeep Soni

Founder, Trainer & CEO, Deccansoft Software Services.

Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.