Thread: SDL_textinput and strcat crah

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    20

    SDL_textinput and strcat crah

    I was following this example (Tutorials/TextInput - SDL Wiki') but strcat text_input crashes the program for some reason, any ideas?


    Code:
        char *text_input;
    
        ...
                case SDL_TEXTINPUT: {
                   strcat(text_input, event.text.text);
                } break;

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In order to strcat to text_input it must be pointing to some allocated memory. It also must contain a valid (possibly zero-length) C-style (i.e., zero-terminated) string.
    Code:
    text_input = malloc(200);
    *text_input = 0;  // set first byte to '\0'
    You would also need to keep track of how much text has been stored in it so far and realloc as necessary, and eventually free the memory. Usually you would learn this kind of thing with console programs and then move on to SDL.

    The example from your link is obviously incomplete. Note the /* ... */. It's also noteworthy that the variables are declared extern, usually indicating that they are defined in another source/object file.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    20
    Thanks that worked. There is another case of pointer trickery if you wouldn't mind helping.

    This is a shared resource: load_texture("textures/btn_gray_active.bmp", &texture_gray_btn);

    Now i want to save texture_gray_btn inside a pointer in a struct, i.e. just the reference so i can free it independantly from the button struct. In this case i want to save it here: SDL_Texture** P_texture_selected;

    I'm not sure how to allocate and deallocate the button and how to properly Query the texture pointer. Any direction would be much appreciated.

    Code:
    typedef struct button {    SDL_Texture** p_texture_normal;
        SDL_Texture** P_texture_selected;
        SDL_Texture* texture_btn_txt;
        SDL_Rect rect_box;
        SDL_Rect rect_text;
        bool selected;
    } struct_button;
    
    struct_button createButton(SDL_Texture** tex_norm, SDL_Texture** tex_select, char* label, TTF_Font *font, int font_size, int x, int y)
    {
        struct_button this;
        this.rect_box.x = x;
        this.rect_box.y = y;
        this.p_texture_normal = tex_norm;
        SDL_QueryTexture(/* The pointer goes here*/, NULL, NULL, &this.rect_box.w, &this.rect_box.h);
    
    
    
    
        this.selected = false;
        return this;
    }
    
    
    void destroyButton(struct_button this)
    {
    
    
    }
    and a test would call the function like so:

    Code:
        struct_button someButton = createButton(&texture_main_menu, NULL, "\0", NULL, 0, 0, 0);    
       printf("%d %d\n", someButton.rect_box.w, someButton.rect_box.h);
    I'm thinking this is the correct way of doing it, but I'm not sure.
    Code:
        this.p_texture_normal = *tex_norm;
    Last edited by Tiago Redaelli; 01-11-2017 at 07:03 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It seems to be the double-pointers that are confusing you, but I don't understand why the textures are double pointers in the first place. They should probably be single pointers. Maybe you're confused by the double-pointer that is used in the load_texture function, but it uses a double pointer there so that it can modify the pointer in the caller.

    You could even get rid of the double-pointer in load_texture by passing back a pointer instead of passing in a double-pointer. This seems more natural to me:
    Code:
    SDL_Texture *load_texture(const char *name) {
        SDL_Texture *t;
        ...
        return t;
    }
    
    SDL_Texture *texture_gray_btn = load_texture("textures/btn_gray_active.bmp");
    BTW,
    Calling your button "struct_button" eliminates the advantage of the typedef since without the typedef it would be called "struct button" (exactly the same but without the underscore). Structs are usually named with the first letter of each word in uppercase, so Button would be a good name:
    Code:
    typedef struct Button {
        ...
    } Button; // it's okay for this to be the same as the struct tag.
    Also, you don't need to write "\0". A plain old "" will do since a string literal adds a zero-byte at the end automatically.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    20
    Thanks for the pointers.

    The reasoning behind double pointers, is that i want to be able to free the texture idependantly of the Button or if multiple Buttons share the same texture, but as you say they are pointers. maybe... it would already work like that.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Tiago Redaelli View Post
    The reasoning behind double pointers, is that i want to be able to free the texture idependantly of the Button or if multiple Buttons share the same texture.
    I can't see how double-pointers would help with that. Try the single pointers and see how it goes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-24-2013, 08:58 AM
  2. My strcat
    By CStrong in forum C Programming
    Replies: 6
    Last Post: 06-13-2012, 07:10 AM
  3. strcat
    By TGM76 in forum C Programming
    Replies: 3
    Last Post: 07-27-2010, 07:42 AM
  4. strcat
    By linuxdude in forum C Programming
    Replies: 1
    Last Post: 12-11-2003, 04:55 PM
  5. strcat
    By dashing_76 in forum C Programming
    Replies: 3
    Last Post: 03-22-2002, 02:01 AM

Tags for this Thread