Monday 24 February 2014

Interesting relationship between Pointers and Arrays


Most of us have worked with Java and other object oriented programming languages and this has exposed us to the world of Arrays which are simply a collection of variables stored sequentially in memory.
For those familiar with C, pointers is an interesting concept. Pointers are variables that store references to other variables.

The declaration and definition, int *ptr =&a causes variable ptr to hold the address of ‘a’ or a reference to ‘a’.

All of this may seem trivial and rather unimpressive. But, the idea of pointers has some interesting ramifications in the world of programming itself and what we will explore today is the relationship between Arrays and Pointers.

Let’s make a simple array definition.
int[ ] a = {1,2,3,4,5,6};

Now, let’s have a pointer ‘p’ refer to the first item of this array;

int* p = &a[0]; ........(1)
What we have now done is stored a reference to the first item of array a in p. Let’s say the six array elements are stored from addresses, 1000 through 1005.

That will store 1000 in p. Now, it is known to us that the ith element of an array, may be accessed using the expression, array[i]. Let’s say we want to access the 0th element of the array, in this case, ‘a’. That could be done by simply saying, a[0]. Now, let’s say we want to access the element of this array at index 4. That would be possible with a[4].

While all this may seem really trivial, we will now see how similar accesses to the array elements may be done using pointers. We just created a pointer, p that references the 0th element of a. Now, we may also access the contents here using the expression *p (value at address stored in p) or *(p+0). If you want to access the content at first index of ‘a’, you can do that with *(p+1). In general, the ith element of ‘a’ may be accessed using *(p+i).

Thus, we can see that the following expressions are equivalent:
a[0] ó *p or *(p+0)
a[1] ó *(p+1)
a[2] ó *(p+2)
and in general: a[i] ó *(p+i)

Now, it is a good idea to recall that the pointer, ‘p’ is itself a variable whereas the array name ‘a’ is just a synonym or a name equivalent for the address of the first element of the array.

In fact, the reference a[i] may be alternately written as *(a+i) because each time you write a[i], C itself converts it to *(a+i). Now, the initial pointer definition in (1) could have been alternately written as int* p = &a for, a[0] is simply *(a+0).
Hold on. Does that suggest ‘a’ is itself a pointer? Well, technically, No. Remember that pointers are variables. Here, ‘a’ is actually more like value that cannot be reassigned, so operations like a++ and assignment statements like a = 3 are illegal.

All that said, *(p+i) may be alternately written as p[i] and this is same as a[i]. In general, the array name and index expression is equivalent to the pointer and offset expression. The latter is determined to be marginally faster, so, it is not a bad idea to use the pointer and offset method for any way, C has to internally convert the array name and index expression to the pointer and offset expression.






No comments:

Post a Comment