# Thread: 'double *' to 'double'

1. ## 'double *' to 'double'

hi

I am using gluUnProject to generate some opbject coordinates. I want to use these in a glVertex2d call to draw some lines on the screen. however, gluUnProject returns parameters of type double * and glVertex2d takes parameters of type double. How can I change it from double * to double? does it involve dereferencing it?
I really have no idea what I need to do here, and can't find any information on it.

any help would be most appreciated

thanks
Gareth

2. ## Re: 'double *' to 'double'

Here is an example (I use this in my picking function):

GLdouble x, y, z;

gluUnProject ( static_cast<double> ( m_x ),
Invert_m_y,
Inverted mouse y.
Result.z,
Model,
Projection,
Viewport,
&x,
&y,
&z );

You are passing in pointers to x, y, z to gluUnProject. It will fill those memory locations with the required values. You simply use x, y, z as you would any ordinary double.

ie. :

GLdouble Distance = sqrt ( x * x + y * y + z * z );

etc....

[This message has been edited by Robbo (edited 04-25-2002).]

3. ## Re: 'double *' to 'double'

Hello!

Say you have a variable x that is of type double * then you can access the value with *x.
Example :
double *x;
x==pointer to the memory address of containing the value of x.
*x==value of x.

Osku

4. ## Re: 'double *' to 'double'

Not quite. Look:

double *x, y;

x is undefined - so could point to anywhere. Initialised to y looks like this:

x = &y; (x points to address of y).

now, *x = 12345.0 and then print out the value of y,

y = 12345.0

Because you put 12345 into the address that x points to (which is y) - so y now equals that value.

When you use gluUnProject, you are passing in the addresses of 3 variables so that OpenGL can store them in the place you want them. The variables themselves are GLdoubles, you just pass pointers to them into gl, ie. &x, &y and &z.

Hope this explains things somewhat.

5. ## Re: 'double *' to 'double'

If you use
Code :
`GLdouble x, y, z;`
to define x, y, z then you don't need to dereference them. In this case your call to gluUnproject looks like
Code :
`gluUnproject(..., &amp;x, &amp;y, &amp;z);`
and you use the values as shown in the following example.
Code :
`len = sqrt(x * x + y * y + z * z);`
If you used
Code :
`GLdouble *x, *y, *z;`
to define x, y, z then you have to dereference them to get their data. In this case your call to gluUnproject looks like
Code :
`gluUnproject(..., x, y, z);`
and you use the values as shown in the following example.
Code :
`len = sqrt(*x * *x + *y * *y + *z * *z);`

6. ## Re: 'double *' to 'double'

The problem with your second method, Furrage, is that you aren't properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.

Always, always, ALWAYS, think about how you're using memory when using pointers. It will save you tons of headaches.

[This message has been edited by Deiussum (edited 04-25-2002).]

7. ## Re: 'double *' to 'double'

Pointers need to either be initialized by allocating memory to it such malloc or the new operator or you assign the address of an existing allocated memory address...

So
GLfloat *x, *y, *z;

x = new GLfloat;// new GLfloat[size]; //Array
y = new GLfloat;// new GLfloat[size]; //Array
z = new GLfloat;// new GLfloat[size]; //Array

or

GLfloat w;
x = &w;

Pointers dont hold values, they hold a memory addresses.

8. ## Re: 'double *' to 'double'

Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk's purpose he should use the second alternative.

9. ## Re: 'double *' to 'double'

Oh yeah Jimmi... At least I expect the programmer to know this if they are using it... Can't use it, without knowing it.
But I guess I should have posted that extra line of code for completeness.

Originally posted by jimmi:
Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk's purpose he should use the second alternative.

10. ## Re: 'double *' to 'double'

Originally posted by Deiussum:
The problem with your second method, Furrage, is that you aren't properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.
Duh... I thought that was so standard that I didn't need to mention it. Anyway, its good you mentioned it, as someone might have thought it was the complete code and get no end of errors if they tried to copy it

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•