Arrays decaying into pointers

&a[1] and &a+1. Are they same or different?

Different. &a[1] is the same as (a+1). In general, x[y] is by definition equivalent to *(x+y).

I wanted to know how *a[5] is represented in memory. Does *a is a base
pointer that points to a[0],a[1],a[2],a[3],a[4].

In your second example, a is an array of pointers. *a[i] is the value of the object, the address of which is stored as the ith element in your array. *a in this case is the same as a[0], which is the first element in your array (which is a pointer).

a=v //why this gives error

Because a (in your last example) is a pointer to an array. You want to assign to a, then you need to assign the address of the array v (or any other array with correct dimensions);

a = &v;

This is very good that you’ve commited to understanding things, but nothing will help you better than a good C book.

Hope this helps.

Leave a Comment