strcat messing things up?!

This is a discussion on strcat messing things up?! within the C Programming forums, part of the General Programming Boards category; I am having problems with this little program. It should output something like 33 p1, 29 p1, each one properly ...

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    strcat messing things up?!

    I am having problems with this little program. It should output something like 33 p1, 29 p1, each one properly positioned inside a cell, but because of the line with strcat, ) it is getting messed up and I get the output as here:

    <pre>
    | - |
    +----------+
    | - |
    +----------+
    | |
    +----------+
    | 33 p1 |
    +----------+
    |22 p133 p1 |
    +----------+
    </pre>


    Code:
    /* Crie um programa que implemente duas pilhas em um único vetor, que deve
     * receber no máximo 10 elementos e a pilha 1 (p1) deve ser implementada da
     * esquerda para a direita, já a pilha 2 (p2) deve ser implementada da
     * direita para a esquerda. */
    
    #include <stdio.h>
    #include <stdio_ext.h>
    #include <string.h>
    
    #define STACKSIZE 10
    
    struct StackStruct {
        short int top1;
        short int bot1;
        short int top2;
        short int bot2;
        char nums[STACKSIZE][5]; // Números de no máximo 5 caracteres.
    };
    
    short int showMenu(void);
    void iniStruct(struct StackStruct *p);
    void insIntoStackOne(struct StackStruct *p);
    void insIntoStackTwo(struct StackStruct *p);
    void showStack(struct StackStruct *p);
    
    int main(void) {
    
        struct StackStruct Pilha;
        short int opt;
    
        iniStruct(&Pilha);
        do {
    
            opt = showMenu();
        
            switch (opt) {
                case 0:
                    break;
                case 1:
                    insIntoStackOne(&Pilha);
                    break;
                case 2:
                    insIntoStackTwo(&Pilha);
                    break;
                case 3:
                    showStack(&Pilha);
                    break;
                default:
                    printf("Something went wrong.\n");
            } // fim switch().
    
    
        } while (opt != 0);
    
        return 0;
    }
    
    void iniStruct(struct StackStruct *p) {
        p->bot1 = -1;
        p->top1 = -1;
        p->bot2 = 10;
        p->top2 = 10;
        for (short int k = 0; k < STACKSIZE; k++) {
            strcpy(p->nums[k], "-"); // 45 é o char '-'.
        }
    }
    
    short int showMenu(void) { // {{{
    
        short int opt;
    
        printf("+-------------------------------+\n");
        printf("| ---- SATCK STUDIES IN C ----- |\n");
        printf("+-------------------------------+\n");
        printf("| %24s -- %d |\n", "Insert in Stack ONE", 1);
        printf("| %24s -- %d |\n", "Insert in Stack TWO", 2);
        printf("| %24s -- %d |\n", "Show Stack Content", 3);
        printf("| %24s -- %d |\n", "Remove From Stack ONE", 4);
        printf("| %24s -- %d |\n", "Remove From Stack TWO", 5);
        printf("| %24s -- %d |\n", "Quit Program", 0);
        printf("+-------------------------------+\n");
        printf("--> CHOOSE ONE OPTION: ");
    
        __fpurge(stdin);
    
        do {
            scanf("%hd", &opt);
            __fpurge(stdin);
        } while (opt < 0 || opt > 3);
    
        return opt;
    
    } // showMenu() fim. }}}
    
    void insIntoStackOne(struct StackStruct *p) {
    
        if ((p->top1 + 1) < p->top2) {
    
            printf("top1: %hd, top2: %hd\n", p->top1, p->top2);
            p->top1++;
            printf("Type a number for stack one: ");
            scanf("%s", p->nums[p->top1]);
            __fpurge(stdin);
            strcat(p->nums[p->top1], " p1");
            printf("%s\n", p->nums[p->top1]);
        }
    
    } // fim insIntoStackOne().
    void insIntoStackTwo(struct StackStruct *p) {
        if ((p->top2 - 1) > p->top1) {
            printf("top2: %hd, top1: %hd\n", p->top2, p->top1);
            p->top2--;
            printf("Type a number for stack two: ");
            scanf("%s", p->nums[p->top2]);
            __fpurge(stdin);
        }
    }
    
    void showStack(struct StackStruct *p) {
        //for (short int i = p->top1; i > p->bot1; i--) {
        for (short int i = STACKSIZE - 1; i > p->bot1; i--) {
            printf("\t+----------+\n");
            printf("\t|%8s  |\n", p->nums[i]);
        }
        printf("\t+----------+\n");
        getchar();
    }
    
    /*
     * vim: set foldmethod=marker foldmarker={{{,}}}:
     */

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You have a buffer overflow. nums isn't big enough. It's a 2-d array that is 10 rows long by 5 chars wide. You need to change 5 to something larger. You're not leaving enough space for the null terminator after you concatenate " p1" on to the end. Basically, you end up with 6 chars, '3', '3', ' ', 'p', '1' and '\0'. strcat add that null terminator, but there's no room, so it overflows into the next row of nums. When you scanf into the next row, you overwrite the null terminator for the previous row, so it appears the two strings are smashed together, and get the output you see.

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    You have a buffer overflow. nums isn't big enough. It's a 2-d array that is 10 rows long by 5 chars wide. You need to change 5 to something larger. You're not leaving enough space for the null terminator after you concatenate " p1" on to the end. Basically, you end up with 6 chars, '3', '3', ' ', 'p', '1' and '\0'. strcat add that null terminator, but there's no room, so it overflows into the next row of nums. When you scanf into the next row, you overwrite the null terminator for the previous row, so it appears the two strings are smashed together, and get the output you see.
    You're right. That completely ignored that fact. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nanosleep messing things up.
    By bungkai in forum C Programming
    Replies: 5
    Last Post: 03-30-2012, 02:37 AM
  2. messing around with bitmaps
    By seanJ in forum C Programming
    Replies: 18
    Last Post: 01-04-2012, 03:52 PM
  3. CWinThread messing itself up
    By LowlyIntern in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 07:08 AM
  4. Is someone messing with m$ again?
    By exluddite in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-16-2004, 01:56 PM
  5. Do you think this might be messing up my brain?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-21-2002, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21