ASD Course - Chap9 - Pointers and Array in C - Relationship and Use
ASD Course - Chap9 - Pointers and Array in C - Relationship and Use
Chapter 9: C pointers
int arr[5];
It declares an integer array with a capacity of five elements. To access any element
of the given array we use array index notation. For example to access zeroth
element we use arr[0], similarly to access fifth element we use arr[4].
The above statement declares an integer pointer pointing at zeroth array element.
1
Algorithmic and DYNAMIC data structure Chapter 9: C pointers
This special behaviour of array allows many interesting things to happen. Things
such as you can interchangeably use array and pointers. You can also use array
name as a pointer pointing at zeroth element. In context of array and pointer, all
of the following statements are valid.
int arr[5];
int * ptr = arr;
ptr = arr; // ptr and arr both points to 0th element of array
*ptr = 100; // Stores 100 at 0th element of array (Since ptr points at arr[0])
*arr = 100; // Stores 100 at 0th element of array
2
Algorithmic and DYNAMIC data structure Chapter 9: C pointers
In the above image first array element i.e. arr[0] is allocated at memory 0x1000. For
the above case I have assumed integer size as 4 bytes. Hence, next array element
i.e. arr[1] will get memory at 0x1004 and so on fifth array element is allocated
at 0x1016.
Since array elements are stored sequentially, hence you can easily apply pointer
arithmetic to iterate though elements. You can use following pointer arithmetic
operations to access array elements.
int arr[5];
int * ptr = arr; // Integer pointer pointing at arr[0]
3
Algorithmic and DYNAMIC data structure Chapter 9: C pointers
#include <stdio.h>
int main()
{
int arr[SIZE]; // Declare an array of size 10
int *ptr = arr; // Pointer to first element of integer array
int i;
return 0;
}
Output –
The condition while(ptr < &arr[SIZE]) will iterate till ptr points withing array range.
In statement scanf("%d", ptr); you might have noticed that, I have not used
address of operator & with variable name in scanf() function. This is
because scanf() function accepts memory address and pointer
4
Algorithmic and DYNAMIC data structure Chapter 9: C pointers
int main()
{
int arr[] = {10, 20, 30, 40, 50}; // Integer array
int *ptr = arr; // Pointer to 0th element of array
/*
* If arr behaves as a constant pointer then compiler
* must complain about arr++. Since arr++ is equivalent to
* arr = arr + 1 which is not permitted.
*/
arr++; // Error
// No error
ptr++;
return 0;
}
However, the above proof is not adequate to say array name is a constant pointer
to zeroth element of array. If it is a constant pointer then sizeof(arr) must return
size of constant integer pointer, but it doesn’t.
5
Algorithmic and DYNAMIC data structure Chapter 9: C pointers
Array name when used directly behaves as a constant pointer but it is not a
constant pointer. Pointer and array both are different concepts though
interlinked.
As we saw earlier, we can use array name as a pointer pointing to zeroth element.
The array dereferencing operator [ ] internally converts array indexed accessing, in
the context of pointers. For example,
We can also write the statement *(arr + 0) as *(0 + arr), since additions are
associative in nature. Which in array indexed format can be written as 0[arr].
Conclusion: You can interchangeably use arr[0] as 0[arr]. Both convey the same
meaning in C programming.