C Array name is base address
This tutorial is just to show how array name holds a base address of an array.
Array name is base address
Consider the below example of an array in c programming -
#include<stdio.h> void main() { char s[]="cat"; int i; for(i=0;s[i];i++) printf("\n%c %c %c %c",s[i],*(s+i),*(i+s),i[s]); }
Answer -
c c c c a a a a t t t t
Explanation :
- Array name is the base address of the array, here ‘s’ holds the base address of an array.
- ‘i’ is the index number/displacement from the base address.
- Array arr[i] can be Represented as -
arr[i] = *(array name + displacement) = *(arr + i) = Value at (Array name + Displacement) = Value at (Base Address + Displacement)
similarly we can conclude that -
s[i] = *(s + i) i[s] = *(i + s)
Consider the above example with practical example, Suppose we have an array having starting address 1000 -
Now in this case array ‘s’ represents an array of character so we can say that ‘s’ holds a base address of a character array.
Representation | Explanation | Expression | Value |
---|---|---|---|
s[i] |
First element of array | *(s + 1) |
'a' |
i[s] |
First element of array | *(s + 1) |
'a' |
*(s+i) |
First element of array | *(s + 1) |
'a' |
*(i+s) |
First element of array | *(s + 1) |
'a' |