![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
relation between array and pointer
how would you guys define the relation between arrays and pointers? how are these two related?
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Pointers, including what is NOT a pointer.
__________________
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#4 |
|
Programmer
|
Narue: thanx for the brief explanation. yea i know, i guess i should get a good c++ book
DaWei: helpful as always, awesome webpage |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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}
...
...
...
0x2000 int *pArray = myArray
Contents at memory address 0x1000: 0
Contents at memory address 0x1014 (presuming 4-byte int): 5
Contents at memory address 0x2000: 0x1000
0x10000 int i = myArray [5];
Action: get address of myArray (0x1000)
add 5 * sizeof int (0x1014, presuming 4-byte int)
get value from 0x1014
put value in 0x10000
Contents at memory address 0x10000: 5
...
0x20000 int j = pArray [5];
Action: get address of pArray (0x2000)
get value from pArray (0x1000) -- extra step, dereferencing
add 5 * sizeof int (0x1014, presuming 4-byte int)
get value from 0x1014
put value in 0x20000
Contents at memory address 0x20000: 5
__________________
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,260
Rep Power: 5
![]() |
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 operation Things 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;
pointer[3] = SomeOtherValue;
*(pointer + 3) = SomeOtherValue;
*(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)
{
x[3] = SomeValue; // ASSUME x is the address of the first element of an array
// and ASSUME that array has 4 or more elements
}
int main()
{
SomeType *pointer;
SomeType array[5];
SomeType aValue;
f(array); // OK. The assumptions made by f() are valid
f(pointer); // Undefined behaviour. pointer is not initialised to point at anything
pointer = array;
f(pointer); // OK. The assumptions made by f() are valid
pointer = &aValue;
f(pointer); // Undefined behaviour. pointer contains a valid address
// but that address is not the first element of an array
// of 4 or more elements.
}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). |
|
|
|
|
|
#7 |
|
Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Array of Strings | Dietrich | C++ | 23 | Oct 7th, 2006 10:04 PM |
| Assigning an array of lists | deanosrs | C | 42 | Apr 13th, 2006 2:35 PM |
| pointer to array of a class | sackarias | C++ | 2 | Mar 28th, 2006 9:04 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 6:36 AM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 11:39 AM |