Array goes of out of bounds without giving any errors

My professor asked me to make a Codebreaker game in C. (User is breaking the code by guessing original code. original code is given as a cmd-line arg.After every attempt;(b, w): the number of correct colors in the correct positions (b) and the number of colors that are part of the code but not in the correct positions (w) are printed as Feedback.)Only standard input and output is allowed. I got it working, but the arrays Secret_Code2 and guess2 goes out of bounds. It has some strange behaviours like changing int variables causes changes in arrays even they are independent. I'm aware that C does not check array bounds, is there any improvements that i can make? Here is my code;

#include <stdio.h>

#define Max_Attempts 12
char *Sectret_CODE = NULL;

int main(int argc,char **argv)
{

    //Definitions
    printf("Available Colors: (B)lue (G)reen (O)range (P)urple (R)ed (Y)ellow\n\n");

    //Getting input and validating
    if(argc != 2)
    {
        fprintf(stderr,"Invalid input\n");
        return 1;
    }
    
    
    Sectret_CODE = argv[1]; 

    int i = Max_Attempts;
    int Won = 0;
    while (i > 0 && !Won)
    {
        int b = 0, w = 0, t=0;
        char guess[4]; 
        char Sectret_CODE2[4];
        char guess2[4];
        printf("No. guesses left: %i\n",i);
        printf("Enter Your Guess: ");
        scanf("%s",guess);
        //printf("%s",guess);
        for(int j = 0; j < 4; j++)
        {
            //printf("%s,%s\n",Sectret_CODE2,guess2);
            if(Sectret_CODE[j] == guess[j])
            {
               b++; 
            }
            else
            {
                Sectret_CODE2[t] = Sectret_CODE[j];
                guess2[t] = guess[j];
                t++;
                printf("%s,%s,%i\n",Sectret_CODE2,guess2,t);
            }

        } 
        int s = t;
        //printf("%i",t);
        Sectret_CODE2[t] = '\0' ;
        guess2[t] = '\0' ;
        

        if(b == 4)
        {
            printf("You Won\n");
            Won = 1;
            return 0;
        }  
        else
        {
            for(int j = 0; j < s; j++)
            {
                for(int k = 0; k < s;k++)
                if(Sectret_CODE2[j] == guess2[k])
                {
                    w++;
                    break;
                }
            }
        }
        printf("Feedback: %i,%i\n",b,w);
        i--;
    }

    if(!Won)
    {
        printf("You Lose!\n");
    }
}



 

Upvotes: 0

Views: 80

Answers (1)

Tom Karzes
Tom Karzes

Reputation: 24052

You aren't allocating space for the terminating null character in your character arrays. Each array needs to hold up to 4 values, plus a terminating null character. So you need to declare them to hold 4+1 = 5 characters. Otherwise writing the null character can write past the end of the arrays.

Also, inside your loop, you are attempting to print those arrays using printf with %s before null-terminating them. You need to null-terminate them, at the proper point, before printing them with %s.

Upvotes: 3

Related Questions