Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2007, 2:55 PM   #1
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Apr 4th, 2007, 2:59 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3 Jimbo is on a distinguished road
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
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 4th, 2007, 8:16 PM   #3
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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
andro is offline   Reply With Quote
Old Apr 5th, 2007, 3:35 PM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
I have never seen anyone solve a pointer like that before, lol.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Apr 5th, 2007, 3:42 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 5th, 2007, 6:23 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Apr 6th, 2007, 6:41 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 7th, 2007, 3:06 AM   #8
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
so for:

int x[10];

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

x[0]

instead of:


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

i am rather curious about this now.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 7th, 2007, 4:35 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Apr 17th, 2007, 12:38 AM   #10
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
thanks for the replies everyone
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 PM.

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