C Array Memory Concept

C Array Memory Concepts

C Array Memory Concept is very important concept.Once again consider the following array.

C Array Memory Concept
C Array Memory Concept

This is how we would declare the above array in C,

int n[] = {25, 33,12,44,56,17};

C Array Memory Concept

We also know that on mentioning the name of the array we get its base address. Thus, by saying *n we would be able to refer to the zeroth element of the array, that is, 25. One can easily see that *n and *( n + 0 ) both refer to 25.
Similarly, by saying *( n + 1 ) we can refer the second element of the array, that is, 34. In fact, this is what the C compiler does internally. When we say, n[i], the C compiler internally converts it to *( n + i ). This means that all the following notations are same:

n[i] *( n + i)
*( i + n)
And here is a program to prove my point.
/* Accessing array elements in different ways */

[c] #include
#include
void main()
{
int n [] = {24, 33,12,44,56,17};
int i;
for (i = 0; i <= 5: i++)
{
printf (“\naddress = %u “, &num[i]);
printf (“element = %d \t%d “, num[i], *( num + i));
printf (“%d “, *( i + num));
}
}
[/c]

The output of this program would be:
address = 4000 element = 25 25 25
address = 4002 element = 33 33 33
address = 4004 element = 12 12 12
address = 4006 element = 44 44 44
address = 4008 element = 56 56 56
address = 4010 element = 17 17 17

Array is defined as the contigious memroy location.The memory address of first array element is 4000.The element stored in there is 25.Similarly, the next memory address is 4002 which has stored 33 in it.

C Programming Resource Book
C Programming Resource Book

This entry was posted in Uncategorized. Bookmark the permalink.