### Thread: Array == address of &Array?(use to be: Array)

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
12
Rep Power
0

#### Array

int ar[11];

As i learned, ar is pointed to the address of ar[0]
or ar==&ar[0].
But surprisely, ar==&ar too.
So that does mean &ar==&ar[0] or ar is ar[0] ( the same address). This is the pont: the value of ar is different from te value of ar[0].

Am I wrong or C is tricky at this point?
Last edited by haison3000; June 12th, 2003 at 08:34 PM.
2. Well now, that's interesting. &ar should be the address of the location holding the address of ar[0] and so should be different -- we would not expect that &ar == ar.

I wrote this test program and compiled it with gcc:
Code:
```#include <stdlib.h>
#include <stdio.h>

int ar[11];

int main(void)
{
printf("&ar[0] = %p\n",&ar[0]);
printf("ar     = %p\n",ar);
printf("&ar    = %p\n\n",&ar);

if ((void*)ar == (void*)&ar[0])
printf("ar == &ar[0]\n");
else
printf("ar != &ar[0]\n");

if ((void*)ar == (void*)&ar)
printf("ar == &ar\n");
else
printf("ar != &ar\n");

return 0;
}```
When I ran it, I got this output:
C:\dcw\PROJECTS\TEST>a
&ar[0] = c830
ar = c830
&ar = c830

ar == &ar[0]
ar == &ar

C:\dcw\PROJECTS\TEST>

The only explanation I can offer is a guess: it appears that since the value of ar is not contained in an actual variable, then &ar is either a meaningless value or else is defined in such a case as being the value of ar. At any rate, I fail to see why &ar would be used if there is no actual pointer variable involved.

To test that, I modified the test program to make ar a pointer variable:
Code:
```#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

int *ar;

int main(void)
{
ar = (int*)malloc(sizeof(int)*11);

printf("&ar[0] = %p\n",&ar[0]);
printf("ar     = %p\n",ar);
printf("&ar    = %p\n\n",&ar);

if ((void*)ar == (void*)&ar[0])
printf("ar == &ar[0]\n");
else
printf("ar != &ar[0]\n");

if ((void*)ar == (void*)&ar)
printf("ar == &ar\n");
else
printf("ar != &ar\n");

return 0;
}```
This time, the output was what we would expect:
C:\dcw\PROJECTS\TEST>a
&ar[0] = 4d180
ar = 4d180
&ar = c828

ar == &ar[0]
ar != &ar

C:\dcw\PROJECTS\TEST>

So the deciding difference appears to be whether ar is an array declaration or an actual pointer variable. Why the first case's results? Beats me.
3. No Profile Picture
status unknown
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
262
Rep Power
12
Originally posted by dwise1_aol
Well now, that's interesting. &ar should be the address of the location holding the address of ar[0] and so should be different -- we would not expect that &ar == ar.
Using an array name in an expression yields a pointer to the first object in the array, and the type of the expression becomes "pointer to T".

There are some operators where this conversion doesn't take place, the unary operator & being one of them. This operator returns a pointer to its operand; when its operand is an "array of type T" it yields a "pointer to an array of type T".

From that we can see that the two should indeed yield the same address.
4. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
13
Code:
```#include <stdio.h>

main()
{
int ar[5] = {5,6,7,8,9};

printf("%p %p %p\n", ar, &ar, &ar[0]);
}```
result:
0xbffffd30 0xbffffd30 0xbffffd30

ar, &ar and &ar[0] all amount to the same value - the same address. no problem.
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
12
Rep Power
0
ar and ar[0] dont have the same address. The point is that C treats the array name is different from pointer although array name can be considered constant pointer to ar[0].
ar actually points to ar[0] but not ar[0].
And address operator(&) applies to array name diffently than to pointer( and variable).
I think BigBadBob is right.
6. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
13
ar and ar[0] quite clearly do have the same address. the address of an array is the address of it's first element. same thing.

the difference between them, is ar is making use of the pointer version of access, and ar[0] the index version of access, and things work differently for both those methods.

if you say ar like that, that automatically puts you in the pointer camp. you're using pointers there: simply because you're not using indexing[0]. if you're using pointers then by default you get direct access to the address value. to get access to the element contents rather than the address you have to use *ar or *(ar+0). *ar and *(ar+0) are same thing, because something plus zero is the same obviously.

if you say ar[0] like that, that puts you in the index camp. and when using indexing, the default you get is direct access to the element contents, rather than the element address. in order to get access to the address while using indexing, you need the & infront of ar[0].

you can switch between the two at the drop of a hat. from one statement to another while using the same array - if you want to do that that is, which you probably wouldn't.

so i think this comes down to switching the array access method, between the index method and the pointer method, and knowing which method you're using.

printf("%p %p\n", ar, &ar[0]);

gives:

0xbffffd30 0xbffffd30

and

printf("%d %d\n", *ar, ar[0]);

gives

5 5

(5 was the int value contained in element 0 of ar)
7. Originally posted by dwise1_aol
Well now, that's interesting.
Very interesting. I would have assumed that &ar would be undefined (when ar is an array, not a pointer to a dynamically allocated array). I have repeated your tests with the same results.

I have never heard about what BigBadBob stated, but it sounds like it is the only reasonable explanation for this. Can any one else confirm that this is part of the language?
8. BigBadBob is close ....

The C and C++ standards literally say that ...

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to a type "pointer to T". The result is a pointer to the first element of the array.

Which says that, if we have the declaration

int ar[7];

then, in an expression,

ar means &ar[0] (as long as you don't do something like ar = x)

&ar is a pointer to to an array of seven ints. The address it points to happens to be that of the first byte in the 7 ints. Which means it points at the same address in physical memory as &ar[0], but it has a different type (pointer to array of seven int).

&ar[0] equates to &(ar[0]) because of precedence rules in the language.
9. No Profile Picture
status unknown
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
262
Rep Power
12
Originally posted by grumpy
BigBadBob is close ....
I don't see where what you said differs from what I said, except that I used fewer words ;-)
10. Great, thanks for the info grumpy. And, indeed, BigBadBob did say the same thing. ;)