• Advertisement

Archived

This topic is now archived and is closed to further replies.

pointer array confusion

This topic is 5029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I was checking out the nehe tutorials when I noticed this code glGenTextures( 3, &texture[0] ); I thought, "But, isn't it a pointer already? What's being passed into the function? The address of an address?" So I ran my own tests, making my own types and making pointer arrays. box *myBoxes[3]; for (int z = 0;z<3;z++) myBoxes[z] = new box; cout << "myBoxes:\t" << myBoxes << endl; cout << "*myBoxes:\t" << *myBoxes << endl; cout << "&myBoxes:\t" << &myBoxes << endl << endl; cout << "myBoxes[0]:\t" << myBoxes[0] << endl; /*cout << "myBoxes[0]:\t" << *myBoxes[0] << endl; (realized i couldn't do that lol)*/ cout << "&myBoxes[0]:\t" << &myBoxes[0] << endl << endl; cout << "myBoxes[1]:\t" << myBoxes[1] << endl; cout << "&myBoxes[1]:\t" << &myBoxes[1] << endl << endl; I received this output: myBoxes: 0012FEC0 *myBoxes: 00322EE0 &myBoxes: 0012FEC0 myBoxes[0]: 00322EE0 &myBoxes[0]: 0012FEC0 myBoxes[1]: 00322F20 &myBoxes[1]: 0012FEC4 I noticed some patterns here such as &myBoxes[0] has the same value as &myBoxes (which is the same as myBoxes), which lead me to find out that I could do glGenTextures( 3, texture ). The problem for me is that I don't totally understand what's going on here? What gets passed into glGenTextures? What IS the value in myBoxes[0]? What IS the value in &myBoxes[0]? Why do they differ? And how come the myBoxes[1] values differ? I am confused! @_@ Educate me, please. (I'm really just on a quest to understand these pointer arrays. I bet there's an obvious explanation for this that I was trying too hard to find.) Thanks very much in advance. [edited by - andyZER0 on April 14, 2004 3:24:11 PM] [edited by - andyZER0 on April 14, 2004 3:25:21 PM] [edited by - andyZER0 on April 14, 2004 3:25:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
array[pos] gives you the object at the given pos.
&array[pos] gives you a pointer to the object at the given pos.

Share this post


Link to post
Share on other sites
array[0] is the first element. &array[0] is the address of the first element. Simply array is equivalent to &array[0]. That is, the name of the array without anything else is a pointer to the first element. In C/C++, all arrays are just pointers (but not all pointers are arrays) pointing to the address of the first element in the array. glGenTextures() takes a pointer to an integer, or equivalently an array of integers. The call could be rewritten with no change in meaning as

glGenTextures(3, texture);

[edited by - aprosenf on April 14, 2004 5:27:05 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Aprosenf
Simply array is equivalent to &array[0]. That is, the name of the array without anything else is a pointer to the first element. In C/C++, all arrays are just pointers (but not all pointers are arrays) pointing to the address of the first element in the array.


You are actually wrong here, though that is an all-too-common misconception. An array is in no way shape or form a pointer! When you make an array all you are doing is constructing a contiguous block of objects of the associated type. Not only is a pointer not created when you make an array, but more subtly, using an array name does not even directly represent a pointer to the first element of the array. It actually just gives you the entire array itself which can implicitly create a pointer to the first element when in proper context (which is where the confusion usually comes in). According to your logic, if you just do:

int array[5];
int** pointer = &myarray;

that should work. In actuality it doesn''t, because your assumption that an array is a pointer is actually false. In actuality, the proper relationship is this:

int (*pointer)[5] = &myarray; // pointer to an array of 5 ints

Another example is by using sizeof

sizeof(myarray);

If myarray really was a pointer like you claim, then the result of that expression would be the size of a pointer! The actual result is 5 * sizeof( int ).

Arrays are not a pointers, they are their own types with convenient ways of creating pointers from them through C++''s syntax when the context dictates that an array doesn''t work in the expression while a pointer to the element type does.

A similar relationship is functions. Using a function name alone can yield a pointer to that function. Does that mean that a function itself is a pointer? Again, no. All it means is that through convenient syntax, you don''t have to explicitly take the address. Not understanding these distinction will eventually come up and bite you on the ass in the future.

Share this post


Link to post
Share on other sites
Sorry, you''re completely right. In most cases, an array can be treated like a pointer, but certainly not always. Dynamic arrays created with malloc() or new are just pointers, but static arrays are their own type like you said.

Share this post


Link to post
Share on other sites

  • Advertisement