Thread: Help me understand this

  1. #1
    Shadow12345
    Guest

    Help me understand this

    Here I have an excerpt of code from an SDL tutorial online.
    The URL for this is(look about a quarter down the page):

    http://cone3d.gamedev.net/cgi-bin/in...ls/gfxsdl/tut2


    The actual code is this:
    Code:
    void DrawIMG(SDL_Surface *img, int x, int y) {
    	SDL_Rect dest;
    	dest.x = x;
    	dest.y = y;
    	SDL_BlitSurface(img, NULL, screen, &dest); 
    }
    I don't understand why the '&' is needed before dest on the last line. I thought &'s were used when making references and assigning pointers, i.e
    int *x = &age; //a pointer
    int& rx = age; // a reference
    dest is not a pointer or reference, it is just an object of the struct SDL_Rect (which means rectangle by the way).

    If you can shed some light into my heathenistic brain I'll probaly sdfj;acibucparcrap

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Actually reading that part on my learning process.

    If I understood it correctly dest is being passed as a pointer to the function. It's still a pass-by-value method, but with the added ability to actually change the caller argument. In this case your dest object.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Shadow12345
    Guest
    dest isn't passed into the function because it is created within the function.

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    dest is being created in DrawIMG and being passed to SDL_BlitSurface.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Shadow12345
    Guest
    Yes dest is created in drawimg and it is passed into SDL_BlitSurface, but it isn't a pointer or reference, right? If it were a pointer I think the declaration would look something like this:
    Code:
    void DrawIMG(SDL_Surface *img, int x, int y) {
    	SDL_Rect* dest = new SDL_Rect;
    	dest.x = x;
    	dest.y = y;
    	SDL_BlitSurface(img, NULL, screen, &dest); 
    }

    If it were a reference I think it would look something like this:
    Code:
    void DrawIMG(SDL_Surface *img, int x, int y) {
    	SDL_Rect& dest; //**
    	dest.x = x;
    	dest.y = y;
    	SDL_BlitSurface(img, NULL, screen, &dest); 
    }
    
    )I'm not totally sure if that is how you declare a reference, but I know there would be a & in there somewhere if it was being declared as a reference)

    Seeing as how it is declared like this:
    SDL_Rect dest;
    as opposed to the examples above it doens't look like a pointer or reference, thus it doesn't make sense to use a & in this line of code:

    SDL_BlitSurface(img, NULL, screen, &dest);

    Ok, I sure hope we understand each other because I want to get this little thing sorted out.

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Shadow12345
    Ok, I sure hope we understand each other because I want to get this little thing sorted out.
    Hehe... It shouldn't be long some hotshot comes here and probably explain it as it is.

    But untill then... the & pointer operator is nothing more than telling the compiler you want to pass the memory location of the parameter.

    so, you can make a call like this...

    int c = 10;
    myfunction(1, 'b', &c);

    and the prototype for this function would be:

    void myfunction(int x, char y, int *z);

    This is exactly the same (I believe) to:

    int a= 10;
    int *c;
    c = &a;
    myfunction(1, 'b', c);

    In this case you are NOT passing by reference, but by value with the added ability to actually change the value of the parameter.

    So, if myfunction does the following:

    void myfunction(int x, char y, int *z)
    {
    *z = 22;
    }

    Then when you exit the function, variable 'a' will indeed be 22 and no longer will it be 10.

    With a class, there's no need to declare the pointer for this type of pass-by-value, since the object properties declarations are already inside the class definition. So, again if I'm understanding what i'm studying right now, you can shorthand it to the form you see on your code.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  7. #7
    Shadow12345
    Guest
    Ok, I think that is what the other person may have been trying to say to me, and it makes a bit more sense, although I will have to read it a few times for it to sink in.

    So basically the constructor of the class SDL_Rect declares the newly-created object to be a pointer? and because of this you can pass it it along using the &?
    Exactly how can the constructor declare the newly created object to be a pointer?

  8. #8
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Shadow12345
    So basically the constructor of the class SDL_Rect declares the newly-created object to be a pointer?
    Nope... The pointer is being declared on the function itself. Take a look at this:

    int a = 10;
    void myfunc(a);

    Here we are just creating a variable, assigning a value to it and using it as an argument to myfunc(). Nothing new.
    Now, let's take a look at myfunc...

    void myfunc(int x)
    {
    //... do something
    }

    variable a is being passed by-value to myfunc. This function declares an integer named 'x'. The value of a is passed to this new variable named x. This is the traditional (read common) way of using functions you see all over.

    Now, look at this:

    int a= 10;
    int *c;
    c = &a;
    myfunc(1, 'b', c);

    Here a pointer (c) is being declared and initialized to the memory address of a... that is c is equal to &a

    So... effectively,

    myfunc(1, 'b', c);

    is about the same as writting

    myfunc(1, 'b', &a);

    Got it?

    The pointer is being declared on myfunc definition. Just like the example above were myfunc declares variable x, myfunc prototype will be...

    void myfunc(int x, char y, int *z);

    That is:
    Pointer z is declared and it will accept &a as an argument.

    which is exactly the same as:

    pointer z is declared and it will accept the pointer c as an argument. (Take a look above were I declare the pointer c and initialize it to &a).
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  9. #9
    Shadow12345
    Guest
    Ok, that all makes sense. I found the prototype for the SDL_BlitSurface function, it is this:
    Code:
    int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,
                            SDL_Surface *dst, SDL_Rect *dstrect);
    in the function pointers to two sdl_surfaces and sdl_rects are declared. When we passed in dest we had to pass it in with the & because that is what agrees with the function prototype.
    If SDL_BlitSurface had been this:
    Code:
    int SDL_BlitSurface(SDL_Surface src, SDL_Rect srcrect,
                            SDL_Surface dst, SDL_Rect dstrect);
    then we wouldn't have to use the & to pass values.

    It doesn't seem all that complicated now, unless I missed something. Thanks mario.
    Last edited by Shadow12345; 05-23-2002 at 08:01 AM.

Popular pages Recent additions subscribe to a feed