C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 9)
9.
What will be the output of the program if the array begins at address 65486?
#include<stdio.h>

int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}
65486, 65488
65486, 65486
65486, 65490
65486, 65487
Answer: Option
Explanation:

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.

Step 2: printf("%u, %u\n", arr, &arr); Here,

The base address of the array is 65486.

=> arr, &arr is pointing to the base address of the array arr.

Hence the output of the program is 65486, 65486

Discussion:
17 comments Page 1 of 2.

Thinakaran said:   6 years ago
int arr[5] = {1, 2, 3, 4, 5};

printf("Address of arr is %p\n", (void*)arr);
printf("Address of &arr is %p\n", (void*)&arr);
printf("Address of arr + 1 is %p\n", (void*)(arr + 1));
printf("Address of &arr + 1 is %p\n", (void*)(&arr + 1));
and the output:

Address of arr is 0x7fff57266870.
Address of &arr is 0x7fff57266870.
Address of arr + 1 is 0x7fff57266874.
Address of &arr + 1 is 0x7fff57266884.


We find that:
(arr + 1) points to 874 which is 4 bytes away from arr, which points to 870 (I have removed the higher order bits of the address for brevity). An int on my machine takes up 4 bytes, so (arr + 1) points to the second element of the array.

(&arr + 1) points to 884 which is 20 bytes away from arr (points to 870).
(884 - 870 = 14 in hex = 20 in decimal).

Taking the size of int into consideration, (&arr + 1) is 5 int-sizes away from the beginning of the array. 5 also happens to be the size of the array. So, (&arr + 1) points to the memory address after the end of the array.

Why is (arr + 1) and (&arr + 1) different though arr and &arr point to the same location?

The answer - While (arr + 1) and (&arr + 1) have the values, they are different types.
arr is of the type int *, where as &arr is of the type int (*)[size].

So, &arr points to the entire array whereas arr points to the first element of the array.

Rupinderjit said:   1 decade ago
In this case arr and &arr is the same thing. So obviously both points to same address.And as we see the available options, option B is relevant. So do the answer.

%u format specifier is used to print the unsigned value.And we know that address of am any cell of the memory can't be negative(that is, can't be of signed value).

Hope u'll get this.

Saurabh Pandey said:   1 decade ago
Why both arr and &arr are same??

arr contains the address of the first element of the array. ie;
arr = &arr[0]

so &arr should be &(&arr).ie;
&arr = &(&arr)

It means both are different. Can any one explain whether they are different or same? If same, how??

Kundan singh said:   10 years ago
Arr gives the base address and here arr+1 means it gives the address of 2nd element. Here 1 denotes we are adding 2 byte to the base address.

And &arr+1 gives the address of next array of 5 integers means it will gives the address of last integers.

Sanjoy said:   1 decade ago
int arr[] = {12, 14, 15, 23, 45};
If we write arr we get the base address.

&arr gives the base address of the 1st element(12).

We know arr[0]=*(arr+0).
&arr[0]=&(*(arr))=arr.

So both are equal.

Sanjana said:   7 years ago
We say that name of an array is a constant variable that stores the base address of the array. Now the variable must also have an address.

So, (&arrayname) must be different than (arrayname).

Urvashi said:   10 years ago
#include<stdio.h>

int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u\n", arr+1, &arr+1);
return 0;
}

What would be the answer?

Manideep said:   1 decade ago
Yeah, arr=&arr[0]

That is arr stores address of frst element in array (i.e base addres of array) and &arr also gives base address.

If I am wrong Let me Know :)

Shibu said:   1 decade ago
Can anyone explain me the difference between

printf ("%u, %u\n", arr, &arr) ;

and

printf ("%u, %u\n", a+1, &a+1) ;

Please follow question 6 and 9.

Manil Kumar said:   8 years ago
The base address of the array is 65486.

arr, &arr is pointing to the base address of the array arr., i,e same Address 65486.


Post your comments here:

Your comments will be displayed after verification.