
Pointer Representation of Arrays in C: Usage and Examples
Table of Content:
Arrays are closely related to pointers in C programming but the important difference between them is that a pointer variable takes different addresses as value whereas, in case of an array it is fixed.
This can be demonstrated by an example:
#includeint main() { char charArray[4]; int i; for(i = 0; i < 4; ++i) { printf("Address of charArray[%d] = %u\n", i, &charArray[i]); } return 0; }
When you run the program, the output will be:
Address of charArr[0] = 37814048 Address of charArr[1] = 37814049 Address of charArr[2] = 37814050 Address of charArr[3] = 37814051
Note: You may get different address of an array.
Notice, that there is an equal difference (difference of 1 byte) between any two consecutive elements of array charArray.
But, since pointers just point at the location of another variable, it can store any address.
When an array is declared, the compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e address of the first element of the array is also allocated by the compiler.
Suppose we declare an array arr
,
int array[5]={ 11, 12, 13, 14, 15 };
Assuming that the base address of arr is 2000 and each integer requires two bytes, the five elements will be stored as follows:
Here variable arrayName
will give the base address, which is a constant pointer pointing
to the element, arrayName[0]
. Therefore arrayName
is containing the address
of arrayName[0]
i.e 1000
. In short, arrayName
has
two purpose - it is the name of an array and it acts as a pointer pointing towards the first element in the array.
arrayName is equal to &arrayName[0] //by default
We can declare a pointer of type int
to point to the array arrayName
.
int *p; p = arrayName; or p = &arrayName[0]; //both the statements are equivalent.
Now we can access every element of array arrayName
using p++
to move from one element to another.
NOTE : You cannot decrement a pointer once incremented. p--
won't work.
Relation between Arrays and Pointers
Consider an array:
int arrayName[4];
In C programming, name of the array always points to address of the first element of an array.
In the above example, arrayName and &arrayName[0] points to the address of the first element.
&arrayName[0]
is equivalent to arrayName
Since, the addresses of both are the same, the values of arrayName
and &arrayName[0]
are also the same.
arrayName[0]
is equivalent to *arrayName
(value of an address of the pointer) Similarly,
&arrayName[1] is equivalent to (arrayName + 1) AND, arrayName[1] is equivalent to *(arrayName + 1). &arrayName[2] is equivalent to (arrayName + 2) AND, arrayName[2] is equivalent to *(arrayName + 2). &arrayName[3] is equivalent to (arrayName + 3) AND, arrayName[3] is equivalent to *(arrayName + 3). . .
&arrayName[i]
is equivalent to (arrayName + i)
AND,
arrayName[i]
is equivalent to *(arrayName + i)
.In C, you can declare an array and can use pointer to alter the data of an array.
Pointer to Array Example
As studied above, we can use a pointer to point to an Array, and then we can use that pointer to access the array. Let's have an example,
#includevoid main() { int i; int a[5] = {1, 2, 3, 4, 5}; int *p = a; // same as int*p = &a[0] for (i=0; i<5; i++) { printf("%d \n", *p); p++; } }
In the above program, the pointer *p
will print all the values stored in the
array one by one. We can also use the Base
address (a
in above case) to act as pointer and print all the values.
Replacing the printf("%d",*p); statement of above example, with below mentioned statement, Lets see what will be the result.
The generalized form for using a pointer with an array,
*(a+i)
is same as:
a[i]
- Question 1: Can we access the array using a pointer in C language?