Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Oct 12th, 2006, 8:12 AM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 13 n00b is on a distinguished road
Send a message via MSN to n00b
relation between array and pointer

how would you guys define the relation between arrays and pointers? how are these two related?
n00b is offline   Reply With Quote
Old Oct 12th, 2006, 8:26 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 22 DaWei will become famous soon enoughDaWei will become famous soon enough
Pointers, including what is NOT a pointer.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 12th, 2006, 9:04 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 432
Rep Power: 13 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Oct 12th, 2006, 10:17 AM   #4
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 13 n00b is on a distinguished road
Send a message via MSN to n00b
Narue: thanx for the brief explanation. yea i know, i guess i should get a good c++ book
DaWei: helpful as always, awesome webpage
n00b is offline   Reply With Quote
Old Oct 12th, 2006, 10:18 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 22 DaWei will become famous soon enoughDaWei will become famous soon enough
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
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 12th, 2006, 12:58 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 14 grumpy will become famous soon enough
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;
which gives a 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];
declares an array of 5 elements. The individual elements are array[index] where index is an integer between 0 and 4 inclusive.

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];
This is achieved by what is called an "implicit conversion". The name of the array is implicitly converted into a pointer (i.e. the compiler does that for you behind the scenes).

The reverse, however, is untrue .... so we can't do this;
    array = pointer;    //  invalid operation
because a pointer cannot be quietly converted into an array.

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;
will set array[0] to be SomeValue. In other words, this would have the same result as;
     array[0] = SomeValue;
From this, we might assume that dereferencing a pointer and accessing a value by index do the same thing. And, in C and C++, that is exactly true. So the third way of doing this is;
    pointer[0] = SomeValue;
And since we play with a pointer like it is an array, we might take a guess and say that we could do the same thing by dereferencing array using the * notation. And, in fact, C/C++ allow us to do this as well, so we have a fourth way to assign array[0] to be SomeValue;
    *array = SomeValue;
Keep in mind that this is only hunky dory if "pointer" contains the address of array[0]. And we can go further, and use pointer notation to access elements of array, or use array notation to find a variable that is offset from array[0]. For example, all of the following lines set array[3] to be SomeOtherValue;
    array[3] = SomeOtherValue;
    pointer[3] = SomeOtherValue;
    *(pointer + 3) = SomeOtherValue;
    *(array + 3) = SomeOtherValue;
The brackets in the last two lines are necessary because of the precedence of operators in the language: "*pointer + 3" has the same result as "pointer[0] + 3", whereas "*(pointer + 3)" yields the same result as "pointer[3]".

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;
does not make pointer become an array. pointer does NOT have any information about the size of array --- because it only contains the address of the first element of our array. This becomes noticeable when we have a function that accepts a pointer as an argument. For example;
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.
}
This little example illustrates the difference between pointers and arrays. f() ASSUMES that the pointer it receives is the pointer to the first element of an array with 4 or more elements. As long as main() honours that assumption, all works fine. But in cases where main() fails to honour that assumption, the result is what the C and C++ standards call "undefined behaviour" --- anything is allowed to happen (a common symptom is a crash, but whether the program crashes or not, it is still invalid behaviour).

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).
grumpy is offline   Reply With Quote
Old Oct 12th, 2006, 4:38 PM   #7
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 13 n00b is on a distinguished road
Send a message via MSN to n00b
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
n00b 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

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




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

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