Simple pointer question but i'm a noob

This is a discussion on Simple pointer question but i'm a noob within the C++ Programming forums, part of the General Programming Boards category; Ok, Just wondering i know a ptr can equal another ptr, but in this code
the second parameter of glGenTextures ...

Simple pointer question but i'm a noob

Ok, Just wondering i know a ptr can equal another ptr, but in this code
the second parameter of glGenTextures is supposed to be a pointer to a
GLuint, but the address of textureId is passed as the parameter, how come
this works? like textureId is not defined as a pointer to a GLuint, does does this mean that if i want to use a pointer to something of a type i just use &something? Could someone please explain this

Code:

GLuint textureId;
glGenTextures(1, &textureId);

glGenTextures Function

The glGenTextures function generates texture names.
Syntax

Code:

void glGenTextures(
GLsizei n,
GLuint *textures
);

Parametersn
The number of texture names to be generated.

textures
A pointer to the first element of an array in which the generated texture names are stored.

Ok, Just wondering i know a ptr can equal another ptr, but in this code
the second parameter of glGenTextures is supposed to be a pointer to a
GLuint, but the address of textureId is passed as the parameter

That is correct.

Originally Posted by tesla

how come this works? like textureId is not defined as a pointer to a GLuint, does does this mean that if i want to use a pointer to something of a type i just use &something? Could someone please explain this

The & is supposed to be an operator that returns the address of the variable in question. Pointers, as you may know, point to the address where a particular value is stored. So it works. You pass by reference, and the function in question can modify the object that you passed it.

If your particular variable isn't a pointer, and you need to pass it to a function that takes a pointer as a parameter, you add the &. You can put it before or after the variable, IIRC. If your variable already is a pointer, then we wouldn't want to pass the address of the pointer, so we would just pass the variable.

If your variable already is a pointer, then we wouldn't want to pass the address of the pointer, so we would just pass the variable.

Not entirely accurate. Generally you pass by reference (i.e. pointer) when the function needs to modify the original object, whether that is a base type, struct, class, union etc. Sometimes you do pass a pointer to a pointer, in cases where the function may need to create a new object to replace the old one and needs to make sure the caller receives the updated object instead of the original, which may no longer exist due to being deleted.

Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.