![]() |
relation between array and pointer
how would you guys define the relation between arrays and pointers? how are these two related?
|
Pointers, including what is NOT a pointer.
|
>how would you guys define the relation between arrays and pointers? how are these two related?
In object context they're unrelated. In value context an array is often treated as a pointer, so they have similar properties. That's the simple explanation, but fully understanding what it means to you is a lot harder. |
Narue: thanx for the brief explanation. yea i know, i guess i should get a good c++ book
DaWei: helpful as always, awesome webpage |
Despite the identical syntax in this example, the underlying operations are different. The language is trying to simplify your life.
:
0x1000 int myArray [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
A third, and longer, explanation follows.
The difference between pointers and arrays comes down to how they are declared, and the things in common are related to how they are subsequently used. A pointer is a variable which holds the address of something. In the basic form, it is declared using syntax like this; :
SomeType *pointer;An array is a set of elements which are located adjacent to (i.e. beside) each other in memory, and each element is identified by an index. For example; :
SomeType array[5];So, when they are declared, there is no relationship between an array and a pointer. After they are declared, the language allows them to be used interchangeably in some ways. The most obvious one is that the name of an array is (as far as the compiler, and therefore the program being compiled, is concerned) treated like it is a pointer to that array's first element. Because of this, a pointer can be made to point at an array (or that array's first element). For example; :
pointer = array; // same as pointer = &array[0];The reverse, however, is untrue .... so we can't do this; :
array = pointer; // invalid operationThings get even more entertaining when we attempt to dereference a pointer (i.e. access the thing it points at) or to access an element of an array. If we assume the value of pointer is the address of a valid SomeType, that SomeType can be accessed by dereferencing the pointer. If the assignment "pointer = array" was done above, then :
*pointer = SomeValue;:
array[0] = SomeValue;:
pointer[0] = SomeValue;:
*array = SomeValue;:
array[3] = SomeOtherValue;From the above, one might think that a pointer is an array and an array is a pointer. That is wrong. An array as a number of elements. A pointer only contains an address, so the assignment; :
pointer = array;:
void f(SomeType *x)The thing to realise is that, in this example, f() receives a pointer and has no means of detecting if that pointer is really an array or not. So there is no means of determining if an array is passed or not. A consequence of this is that f(), because it can't even determine IF it's argument is an array, it certainly can't extract the size of that array (if any). |
oh maaaan, this is...i dunno what to say but THANK YOU man...i understand the whole thing now, i really do. THANKS!
thanks to DaWei aswell, good to have your examples |
| All times are GMT -5. The time now is 12:34 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC