Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 12th, 2003, 08:05 PM
 haison3000
Junior Member

Join Date: Jun 2003
Posts: 12
Time spent in forums: < 1 sec
Reputation 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 09:34 PM.

#2
June 12th, 2003, 08:35 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,870
Time spent in forums: 3 Months 1 Day 8 h 48 m 58 sec
Reputation Power: 2199
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
June 13th, 2003, 12:40 PM
status unknown

Join Date: Jun 2003
Posts: 262
Time spent in forums: < 1 sec
Reputation Power: 11
Quote:
 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
June 13th, 2003, 02:02 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
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
June 13th, 2003, 02:50 PM
 haison3000
Junior Member

Join Date: Jun 2003
Posts: 12
Time spent in forums: < 1 sec
Reputation 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).

#6
June 13th, 2003, 04:43 PM
 balance
.

Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
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
June 13th, 2003, 08:06 PM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Quote:
 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
June 14th, 2003, 07:48 AM
 grumpy

Join Date: Jun 2003
Posts: 1,044
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 13

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
June 14th, 2003, 09:05 AM
status unknown

Join Date: Jun 2003
Posts: 262
Time spent in forums: < 1 sec
Reputation Power: 11
Quote:
 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
June 14th, 2003, 11:19 AM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Great, thanks for the info grumpy. And, indeed, BigBadBob did say the same thing.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Array == address of &Array?(use to be: Array)