Strings Question [C++]

Started by
2 comments, last by NUCLEAR RABBIT 17 years, 7 months ago
Hello, Im learning SDL and I came across something I havent seen before, c_str(). I have a few questions regaurding c_str(). 1) What does c_str() do? I searched but coudlnt find anything 2) When would you want to use c_str()? 3) In the code below, when i remove c_str() from the line, the code gets an error. Can you please explain why? Lazy Foo Productions Source: (tutorial 2)

#include "SDL/SDL.h"
#include <string>

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image( std::string filename ) 
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    
    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() ); ///// RIGHT HERE \\\\\
    
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
    
    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;    
    }
    
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was in error in setting up the screen
    if( screen == NULL )
    {
        return 1;    
    }
    
    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );
    
    //Load the images
    message = load_image( "hello_world.bmp" );
    background = load_image( "background.bmp" );
    
    //Apply the background to the screen
    apply_surface( 0, 0, background, screen );
    
    //Apply the message to the screen
    apply_surface( 180, 140, message, screen );
    
    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;    
    }
    
    //Wait 2 seconds
    SDL_Delay( 2000 );
    
    //Free the surfaces
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );
    
    //Quit SDL
    SDL_Quit();
    
    return 0;    
}
Advertisement
c_str() is a very useful method of the string class. Normally, most win32 functions and a lot of other API functions don't take strings as argument because they have to be compatible with good old C. These functions take a pointer to a character array instead.

In order to let you still use strings with these functions, the string class incorporates the c_str() method which returns a pointer to the character array that contains the text in the string. By using this method you can use strings in functions that require a char pointer.

For example:

std::string str = "Hello World!";// Messagebox function doesn't take a string as argument// Thus, we use c_str()MessageBox ( NULL, (char*)str.c_str(), "Hello World!", MB_OK );


Hope that clarifies things ;)
the string::c_str( ) function returns a C-style character array representation of the string. This is useful when you are dealing with libraries (like SDL) that do not use the string class.

For example the SDL_LoadBMP function expects a pointer to a character as its argument. You cannot pass it a string because that is not its type. So you call filename.c_str( ) to get a character array from your string and pass that.

Hope that made sense.
thanks for the fast and helpfull replies [smile]

This topic is closed to new replies.

Advertisement