![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#3 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#4 |
|
Expert Programmer
|
I have never seen anyone solve a pointer like that before, lol.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It's just as well you haven't.
Quote:
__________________
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 |
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A longer explanation can be found by referring to the C++ standard:
Quote:
__________________
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 |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
thanks for the replies everyone
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|