Thread: Simple pointer question but i'm a noob

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    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
    );
    Parameters
    n
    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.


    Thanx

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Quote Originally Posted by tesla View Post
    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.

    Quote Originally Posted by tesla View Post
    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.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Zach_the_Lizard View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  4. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 12:48 AM
  5. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM