Archived

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

ELS1

OMG...i understand pointers!

Recommended Posts

ELS1    127
i cant believe that i actually understand pointers now! now i cant live without pointers! it feels nice! oh yea...one quick question! what is the use of the **pointer? i know what i does..but i cant really apply this to a real world project :D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I don''t ussually use the **pointer... I have used it.. but nothing I couldn''t have done with a single pointer... unless you need to dynamically create an array of pointers.. or you''re pointing to an array of pointers. Say, you''re writing a game, and you have pointers to objects... you store these pointers in an array of variable size (depending on how many objects in the array!!), now you could use linked lists... which holds the pointer to the next object (and sometimes the previous object) and have a head/tail pointer.. or you could make an array of pointers with a **pointer. There really isn''t any advantage to it, but that''s a use..

Billy

Share this post


Link to post
Share on other sites
ZeroBit    122
quote:
or you could make an array of pointers with a **pointer. There really isn''t any advantage to it, but that''s a use..



actually i find **pointer are quite useful for quick acessing dynamics multidimensional array.
Accesing a two dimensional array this way is faster because you wont have to recalculate array indexes by i+j*numcolumns everytime you want to access an element

Share this post


Link to post
Share on other sites
JoeyBlow2    100
Here is a use for a **...

Let''s say you are attempting to read in a file with a dynamic list of lines like:


5
grass.bmp
tree.bmp
bush.bmp
water.bmp
flower.bmp


But it could be 10, or 15, or 100000 number of lines.

So you have to declare it "char **"

So you read in the first line, 5.

So then you do this,



char *templine;

templine = fgets(filepointer);

long numberoflines;

numberoflines = atoi(templine);

char **temp = 0;

temp = new char*[numberoflines];

long x;

for(x=0;x{
char* templine;
templine = fgets(filepointer)
temp[x] = new char[strlen(templine)];
strcpy(temp[x], templine);
}

if(temp != 0)
{
for(x = 0; x< numberoflines;x++)
{
delete temp[x];
}
delete []temp;
}




Now you just read in your file that is dynamic list of text lines... No extra memory wasted, using char *''s and char **''s. No hard-coding.

Share this post


Link to post
Share on other sites
Spartacus    122
This might have been said already, but I haven''t read the other posts. One place where pointers to pointers are used, is in the DirectX API. Say you have a pointer to a directdraw interface like this:

IDirectDraw7* pDDraw;

Now, to create a valid pointer to this interface you pass the address of the pointer to the function DirectDrawCreateEx which is declared as follows:

DirectDrawCreateEx( GUID FAR *lpGuid, VOID **ppDD, REFIID iid,IUnknown FAR *pUnkOuter );

Now you could call the function like this:

DirectDrawCreateEx(NULL, &pDDraw, IID_IDirectDraw7, NULL);

This way you actually pass the address of a pointer as the ppDD parameter (the address of the IDirectDraw7 pointer, so ppDD becomes a pointer to another pointer). This will enable the DirectDrawCreateEx() function to allocate memory to pDDraw like this:

(in DirectDrawCreateEx)
*ppDD = new IDirectDraw7;

When writing *ppDD you actually reference the variable pDDraw, so the code above will put a pointer to newly allocated memory in pDDraw.
Hope you understand this...

René

Share this post


Link to post
Share on other sites
bishop_pass    109
Pointers are good. I enjoy both *pointers and **pointers.

However, I personally have never found a use for a ********pointer. Does anyone have any pointers with regard to this?

___________________________________

Share this post


Link to post
Share on other sites
Drizzt DoUrden    100
I still haven''t found the use of pointers, I just understand them and know how to use them. At first I thought they were there so I could create and delete variables, but now I realize that if I just make local variables, they dissapear after the function is done anyway ;0

So when will I need pointers? For AI?

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net''s Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)

Share this post


Link to post
Share on other sites
Peeves    122
I don''t understand pointers, i know what they do, but i am not sure as to why they are useful and how they are good for solving problems and declaring pointers kinda confuses me too, also seeing it in code i don''t realise it.
anyone know a good link to explain pointers?

Share this post


Link to post
Share on other sites
Null and Void    1088
quote:
Original post by ZeroBit
Accesing a two dimensional array this way is faster because you wont have to recalculate array indexes by i+j*numcolumns everytime you want to access an element

Yes you do, it''s basically just hidden from you by the compiler. Also, it is ''easier'' to make it more inefficient since the array may not be (and probably isn''t) contiguous in memory.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
bishop_pass    109
Pointers point to stuff (data). What they point to does not need to be regularly sized as in an array. Also, what they point to does not need to be arranged linearly in memory.

Start playing with sparse dynamically changing trees and suddenly pointers will start to sound really good.

___________________________________

Share this post


Link to post
Share on other sites
Hamster    247
    
//Quick tour of pointers because I have nothing better to do


//For example purposes I'll use the data type int,

//but you could use any in theory


int variable;

//To Create a pointer

int *pointer;
//To assign a pointer

pointer = &variable;

//To access the variable through the pointer

// *pointer

//these are the same thing, really,

//*pointer has the value stored by the memory at the address of

//variable. Therefore *pointer==variable.


//Pointers really become useful in functions.

//supposing I have the following function

//int Add(int x, int y)

//{

// return (x+y);

//}

//When the function is called, the variables submitted as x and

//y have to be copied to x and y so that they can be local

// variables in that function. The result also has to be copied

// to the invocation of the function when it returns. There may

// well be even more to it which I've forgotten, but the point

//is that an awful lot of memory that doesn't need to be used up

//is ruthlessly trampled on. How can we write some clean code

//to get round this problem then? We use pointers.


//this version eliminates the need to copy things as it is using

//the variables that already exist, all you are passing to this

//function are the memory addresses, very useful as memory

//addresses are short, but any size of data type or class or

//structure, or array can be denoted by one pointer.

int Add(int* x, int *y)
{
return(*x + *y);
}


int main()
{
int a, b;
a = 3;
b = 4;

printf("a and b add together to give, %d\n", Add(&a, &b));
return 0;
}


There are other uses of pointers, but if you are currently at the stage where you don't understand pointers they probably come a little later on.

Sorry, I meant to do something short and concise but they are quite a hard topic to cover. the things you need to remember when using pointers though are some of the operators involved.

& means "Address of"
* means "value of" - known as the dereferencing operator

Any C/C++ book should explain pointers fairly well. I imagine a search of the internet could come up with some good resources fairly easily.





"In the beginning, there was nothing... which exploded." - Terry Pratchett

Edited by - hamster on January 12, 2002 1:48:18 AM

Share this post


Link to post
Share on other sites
KingPin    122
My favorite use for the **pointer is in function args:

  
char string[] = "Whazzzup";
char* ptr = NULL;

void main()
{
for(;;)
{
IterateString(&ptr);
if(ptr)
cout << *ptr;
else
break;
}
}

void IterateString(char** ptr)
{
if(*ptr == NULL)
*ptr = string;
else
*ptr = *ptr + 1;
}



Stupid example, but I hope it gets the point across.



"1-2GB of virtual memory, that''s way more than i''ll ever need!" - Bill Gates
"The Adventure: Quite possibly the game of the century!" - The Gaming Community

Share this post


Link to post
Share on other sites
Tjoppen    122
i like these pointers:

void AddToCharList( char *name, char **&list );

THAT is annoying....when you need an & to change the value, and it´s a pointer, so it gets either *& or **&

Share this post


Link to post
Share on other sites
Shannon Barber    1681
quote:
Original post by bishop_pass
Pointers are good. I enjoy both *pointers and **pointers.

However, I personally have never found a use for a ********pointer. Does anyone have any pointers with regard to this?


A sparse matrix of that order can be used for perfect chineese checkers AI.

A **pointer is called a handle is very useful. Sometimes instead of a true pointer, and index is used at the second level, but it''s still called a handle. Like a HWND.

** is also a sort of 2D matrix, and is how [][] works.


I''d go so far as to say you need pointers to code, without them it''s script.

Share this post


Link to post
Share on other sites