Jump to content
  • Advertisement

Archived

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

kag1

pointer to a pointer

This topic is 5224 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

Ok, I''m just getting the hang of pointers now and starting to use them (basically only to change things inside of functions). but what are some of the reasons for using a pointer to a pointer (at least thats what I think it is..''int ** iPoint;'') and if it is actually a pointer to pointer..then why do we just stop there..why don''t we make pointers to pointers to more pointers..(or do we?) well can someone please help me out here, I see no reason to ever use a Pointer to Pointer, and if thats not what it is..then someone please tell me what the double ** means?

Share this post


Link to post
Share on other sites
Advertisement
a pointer to a pointer is called a handle. the win32 API makes lots of use of the concept of the handle. it''s useful for a function like this:


int createAnObject( MyObect ** hObject )
{
*hObject = new MyObject();
}


basically you can''t change the value of a function argument. you _can_ however change the value of what a pointer points to, or what a reference points to.

anwyay, basically handles are used when you want to pass a pointer around that you want to allow other people to modify the value of. maybe it''s a temp pointer and the function will change the value to point to a different object or something. i personally only rarely use handles in programming (in fact not one in my engine at all), or when using the win32 API.

-me

Share this post


Link to post
Share on other sites
Yes it is a pointer to a pointer. One reason it would be used is for arrays of arrays, like array of character arrays (like a list of strings). Yes, you can have pointers to pointers to pointers, but they would be used much less.

Share this post


Link to post
Share on other sites
int x = 5; // an integer, which currently has the value 5 stored in it

int *p1 = &x; // an integer pointer, currently pointing to the x variable, which currently has the value 5

int **p2 = &p1; // a pointer to a pointer to an int, currently pointing to p1, which is currently pointing to x, which is currently 5

int ***p3 = &p2; // I hope you see where this is going ...

you can have as many levels as you want, although the only time most people every go further than ** is when they need multidimentinal arrays ...

for example:

int vector[3]; // array of 3 ints
int matrix[3][3]; // 3x3 matrix of ints (9 total)
int cube[3][3][3]; // 3d matrix of ints (27 total)

pointers and dereferences are like derivatives and integrals ... you can keep going in each direction as far as you want, but the usefull information usually stops 1-2 levels out ...

you would use a pointer, so you can change what object you are looking at ...

you would use a pointer to a pointer, so you can change which object is being looked at by the object you are looking at ...

for example, if you had a C functions like this:

// Do Something With An Int
void foo(int x);

inside foo, you can read the value of x, but if you change it, it does NOT change in the calling routine ... cause foo has a COPY of x - so you do this.

// Do Something To An Int
void foo(int *x);

inside foo, if you write a line like: *x = 6, you have changed the value stored in the int iteself, and this will affect the calling program ... but how would you allocate memory for the caller? so you do this:

// Do Something To A Pointer To An Int
void foo(int **x);

now inside this function you can do stuff like:

**x = 6; // to change the int to 6
// OR
*x = &selectedItemID; // to set the MIDDLE pointer to point to a different int than it originally did ... so now - assume the CALLER had done this:

int *currentID = &defaultItemID;

foo(¤tID); // now current ID points to the selectedItemID variable.

THIS is a bad example as far as usefullness, but I hope it helped.

Share this post


Link to post
Share on other sites
A "pointer to a pointer" is nothing more than a single dimension dynamic array. Xai gave a good example above, with the matrix array.

Think of it like this. You have one looong array, like so:
_____________________________
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
This array only has one dimension, the X axis. To access and store this array, you would use something like this:
int* myArray = new int [15];
cout << myArray[3] << endl;


Now, imagine we chopped up that long 15 int array into one 5x3 array. It would look like this:
__________
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_|

We can access it a new way, using two dimensions: X and Y.

int** myArray = new int* [5];
for(int i=0; i<5; i++){
myArray[i] = new int [3];
}

cout << myArray[2][1] << endl;


So how does that "double array bracket" access work? Well, the short answer is that each array bracket "dereferences" one of those asterisks of the "double pointers" that you asked about.

If you're curious about how that works, wrap your head around this: a two dimensional array is really only a long one dimensional array, accessed by
y * rowLength + x    
.

As far as usefulness, if you were around in the glory days of RPG's on the SNES, with games like Zelda: LTTP or Secret of Mana, Chrono Trigger, etc. you notice that the levels were all made up of square tiles. How do you think they were represented? A 2D array of course! Usefulness really depends on the situation though. In the case of a map editor for one of those terrific games, a 2D array would be ideal.



_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.


EDITS: these darn code boxes keep fowling up!
[edited by - MajorShredd on June 3, 2004 8:55:33 PM]

[edited by - MajorShredd on June 3, 2004 9:01:19 PM]

[edited by - MajorShredd on June 3, 2004 9:03:35 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Palidine
a pointer to a pointer is called a handle. the win32 API makes lots of use of the concept of the handle. it''s useful for a function like this:



I''ve never heard it called a handle. That''s not really what a handle is.

A handle is just an object you use to access or specify a managed resource like a window or icon. You don''t get a pointer to it, you get some ID value that is meaningless except to the manager of the resource.

Win32 uses handles for windows, device contexts, icons, etc. COM interfaces often take addresses of pointers to give back a pointer to a managed resource instead of a handle, so the user can call methods in an OO way. So, they''re often used to do similar things, but they''re not the same. But I''m just being pedantic.

My spoon is too big.

Share this post


Link to post
Share on other sites
Whoa, whoa. A 2d array is not really the same thing as a **. You said the key yourself: "a two dimensional array is really only a long one dimensional array." In other words, it''s still just a single *. In the case of a **, the memory would not (necessarily) be sequential.

Share this post


Link to post
Share on other sites
Like others have said, yes ** means pointer to a pointer. Normally you dont'' use them too much. They are used for multi-deminsional dynamic arrays. Basically resulting in a dynamic arrays of dynamic arrays. I don''t use them too much but I think the last time I used them was when I was writing a memory manager for my kernel. But there are other uses for them. Like if you want a calling function to be able to modify a pointer(but in C++ you can just use a pointer reference). Other than those things I can''t really think of any uses for them. If you had a dynamic array that you watned various parts of your code to have access to you could have parts of your code store a pointer to the pointer to the dynamic array. Although that wouldn''t be a very good coding practice, and wouldn''t be very OOP like. Anyways, enough of my rambling.

Share this post


Link to post
Share on other sites
just to be clear, a poitner to a pointer is not a dynamic array, but can be USED to make a 2 dimensional dynamic array.

lets look at the following:

int *x;
int *y;
// both are "pointers" and more specifically "pointer to int"
x = new int(5);
y = new int[5];
// x now points to a single int holding the value 5.
// y now points to and array of 5 ints, each of which is not yet initialized.
y[0] = 100;
y[1] = 101;
y[2] = 102;
y[3] = 103;
y[4] = 104;
// y now points to an array of 5 ints, set to 5 different values
*x // is the value 5
x[0] // is the value 5
x[2] // is a logical runtime error because you have gone into memory you shouldn''t have - this error may go undetected for years before crashing your program
*y // is the value 100
y[0] // is the value 100
y[2] // is the value 102

so when you see a pointer or pointer to pointer type, that is not enough information to determine if the pointer points to an array or not, it is up to other information in the program to let you figure that out ... such as DOCUMENTATION

a "handle" in the windows sense is simple a typedef for an "int" ... it is a LOGICAL pointer, but not a C/C++ pointer. You ask the OS to give you a handle, and it does, then you use that handle (think of it as an integer database id) in many function calls, for instance, when you create a new Window, windows gives it an ID (say 203 for the sake of arguement) ... you store that for later use .. then when you post a message, you say PostMessage(myWindowHandle, OTHER ARGS HERE), and windows goes and looks up the window with id 203, ... just like a pointer, but it isn''t actually a poitner.

hope you have more information than you ever wanted now

Share this post


Link to post
Share on other sites
quote:
Original post by KrazeIke
Whoa, whoa. A 2d array is not really the same thing as a **. You said the key yourself: "a two dimensional array is really only a long one dimensional array." In other words, it's still just a single *. In the case of a **, the memory would not (necessarily) be sequential.

I thought all arrays were sequential, such that the index into the array brackets was added to the base address of the entire array. If this is not so, how does array indexing work if an array is not contiguous (esp. in the case of a 2D array)?

_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.


[edited by - MajorShredd on June 4, 2004 6:40:50 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!