Thread: Strange behaviour increasing this.cur_size?

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

    Strange behaviour increasing this.cur_size?

    Can't figure out why this.cur_size never increases above 1 when i add characters to this.char. Any ideas whats wrong with the appendInTextBox?

    Code:
    /**     \ TEXT BOX /
    
    
    
        typedef struct TextBox {
            SDL_Texture* texture_normal;
            SDL_Texture* texture_selected;
            SDL_Texture* texture_btn_txt;
            SDL_Rect rect_box;
            SDL_Rect rect_text;
            int cur_size;
            int max_size;
            char* msg;
            bool highlighted;
            bool selected;
            bool show;
        } TextBox;
    
    
    */
    
    
    TextBox createTextBox(SDL_Texture* tex_norm, SDL_Texture* tex_select, TTF_Font *font, SDL_Color txt_color, int x, int y, bool show)
    {
        TextBox this;
        this.rect_box.x = x;
        this.rect_box.y = y;
        if (tex_norm != NULL) {
            this.texture_normal = tex_norm;
            SDL_QueryTexture(this.texture_normal, NULL, NULL, &this.rect_box.w, &this.rect_box.h);
        }
        if (tex_select != NULL) {
            this.texture_selected = tex_select;
        }
        this.highlighted = false;
        this.selected = false;
        this.show = show;
        this.max_size = 10;
        this.cur_size = 0;
        this.msg = malloc(this.max_size + 2);
        this.msg[0] = '\0';
        return this;
    }
    
    
    void appendInTextBox(TextBox this, char* msg_)
    {
        for (int i = 0; msg_[i] != '\0'; i++)
        {
            if (this.cur_size < this.max_size) {
                this.msg[this.cur_size] = msg_[i];
                this.cur_size++;
                this.msg[this.cur_size] = '\0';
            }
        }
        printf("cur: %d, s: %s\n", this.cur_size, this.msg);
    }
    Last edited by Tiago Redaelli; 01-12-2017 at 05:44 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The problem is that you are passing a copy of the TextBox struct data into appendInTextBox, then modifying that copy, then basicaly throwing it away. The usual way to do this is to pass in a pointer. And you don't need the trailing underscore in the msg parameter.
    Code:
    void appendInTextBox(TextBox *this, char *msg) {
        for (int i = 0; msg[i] && this->cur_size < this->max_size; i++)
            this->msg[this->cur_size++] = msg[i];
        this->msg[this->cur_size] = '\0';
        printf("cur: %d, s: %s\n", this->cur_size, this->msg);
    }
    Last edited by algorism; 01-12-2017 at 06:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very strange behaviour
    By Fyzix in forum C Programming
    Replies: 5
    Last Post: 12-02-2012, 11:50 AM
  2. strange behaviour
    By cfanatic in forum C Programming
    Replies: 9
    Last Post: 07-13-2012, 10:41 PM
  3. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 06:44 PM
  4. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 AM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM

Tags for this Thread