C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 12th, 2003, 07:05 PM
haison3000 haison3000 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 12 haison3000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post 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.

Reply With Quote
  #2  
Old June 12th, 2003, 07:35 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 29 m 15 sec
Reputation Power: 462
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.

Reply With Quote
  #3  
Old June 13th, 2003, 11:40 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #4  
Old June 13th, 2003, 01:02 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old June 13th, 2003, 01:50 PM
haison3000 haison3000 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 12 haison3000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).
I think BigBadBob is right.

Reply With Quote
  #6  
Old June 13th, 2003, 03:43 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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)

Reply With Quote
  #7  
Old June 13th, 2003, 07:06 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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?

Reply With Quote
  #8  
Old June 14th, 2003, 06:48 AM
grumpy's Avatar
grumpy grumpy is offline
Left due to despotic ad-min
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Posts: 1,042 grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 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.

Reply With Quote
  #9  
Old June 14th, 2003, 08:05 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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 ;-)

Reply With Quote
  #10  
Old June 14th, 2003, 10:19 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Great, thanks for the info grumpy. And, indeed, BigBadBob did say the same thing.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array == address of &Array?(use to be: Array)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT