## Wednesday, September 7, 2011

### Address of array and Address of first element of array

Today we will explore the address of array and the Address of First Element of the same array....
let us first create an array of int containing the 5 elements.
int a[5]={1,2,3,4,5};
Here a is the array of the 5 int data type elements...
As we know we can find the address of the array by applying the & operator with the array name that is "a",so the address of the aray is "&a"...we can print this address using the %u format specifier in the printf..
printf("address of array is:%u",&a);//address of the array
let us write a program..
```#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5};
clrscr();
printf("The address of array is:%u",&a);
getch();
}
```
The output is the address of the array...let us say it prints
The address of array is:65516
Now let us find the address the first element of the array,as ww know we can get the first element by a[0],and its address by &a[0]..
so the statement printf("The address of first element is:%u",&a[0]); gives the address of the first element of the array...it prints.
The address of first element is:65516
Further,we also know that the a(the name of the array) is nothing but the pointer to the first element of the array so printing the a will also give the same value....

```#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5};
printf("The address of array is:%u",&a);
printf("The address of first element is:%u",&a[0]);
printf("The value of a is:%u",a);
}
```
output:
The address of array is:65516
The address of first element is:65516
The value of a is:65516

As we can see the result the,The conclusion is...
"The Address of Array and the Address of its first element is Same........".
As for the explanation we can say that array starts from the first element so the address of its first elements is same as the address of itself........

So,the Question arises--
"What's the Difference Between Pointer to array and the pointer to the first element of array if both contains the same address????"
that is:
if we declare int a[5];
and a pointer to array that is...
int (*ptr)[5];
ptr=&a;
and a pointer to first element of the array that is....
int *ptr1;
ptr1=a;
so,What's the Difference between ptr and ptr1??????
Get Answer here.

#### Post a Comment

Feel free to comment......