Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Why are these the same? (http://www.programmingforums.org/showthread.php?t=12937)

Fall Back Son Apr 4th, 2007 3:55 PM

Why are these the same?
 
void UseArray (int a [])
void UseArray (int *a)

apparently these are the same. I have a guess why, so I'll say that, and hopefully someone will be kind enough to tell me if I'm wrong (and what the real reason is, if so).

void UseArray (int a []):
Passed the address of the array to the function UseArray.

void UseArray (int *a):
Passes the thing the pointer points to (the value of a) to the function, and by doing so, also passes the address of a.

Jimbo Apr 4th, 2007 3:59 PM

You might want to check out DaWei's or Narue's articles on pointers.

[edit:] They're both really long, but both do address the difference between arrays and pointers

andro Apr 4th, 2007 9:16 PM

If A[N] and *(A+N) mean the same thing...

&A[N] == &*(A+N)

&A[N] == (A+N)

Set N to 0

&A[0] == (A+0)

&A[0] == (A+0) == A

kurifu Apr 5th, 2007 4:35 PM

I have never seen anyone solve a pointer like that before, lol.

DaWei Apr 5th, 2007 4:42 PM

It's just as well you haven't.
Quote:

If A[N] and *(A+N) mean the same thing...
Good thing the IF is there; they don't mean the same thing.

grumpy Apr 5th, 2007 7:23 PM

The short explanation is that, by convention, the C compiler treats the name of an array like it is a pointer and, conversely, a pointer like it contains the address of the first element of an array, in some circumstances.

DaWei Apr 6th, 2007 7:41 AM

A longer explanation can be found by referring to the C++ standard:
Quote:

4.2 Array-to-pointer conversion [conv.array]

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

2 A string literal (_lex.string_) that is not a wide string literal can
be converted to an rvalue of type "pointer to char"; a wide string
literal can be converted to an rvalue of type "pointer to wchar_t".
In either case, the result is a pointer to the first element of the
array. [Note: this conversion is deprecated. See Annex _depr_. ]
For the purpose of ranking in overload resolution (_over.ics.scs_),
The equivalency of the two, under some conditions, is due to a conversion, not an identity. You can see this clearly if you write the relevant code and examine the emitted machine code. This is akin to an integer being converted or promoted to a double, without a cast, if you write the code correctly.

bl00dninja Apr 7th, 2007 4:06 AM

so for:

:

int x[10];

for a lot of operations would it be more efficient to use:

:

x[0]

instead of:

:

x

??????????????????????

i am rather curious about this now.

grumpy Apr 7th, 2007 5:35 AM

Not quite, bl00d. For some use cases, "x[0]" and "*x" have the same meaning. Or, alternatively, "x" and "&x[0]" have the same meaning.

Keep in mind that an array and a pointer are different things. We're talking about cases where the compiler converts one into the other so they appear to be the same.

Fall Back Son Apr 17th, 2007 1:38 AM

thanks for the replies everyone :)


All times are GMT -5. The time now is 2:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC