Jeff
Jeff

Reputation: 353

How can I fix my code that shows me segmentation fault on C?

For my assignment, I am trying to recreate the turtle graphics in C. Basically when you enter a series of commands, my program processes the commands and draws a shape in a 50 by 50 array(floor) accordingly. When I run my program, it keeps on giving me segmentation fault error and I don't know what that means so I can't spot out my mistake.

This is my code:


int facing = 3; //start by facing east
int pen = 1; //start by pen up
int copyToArrays(int commandCheck[2][2], const int commands);
int readCommand(int commandCheck[]);
void performCommand(const int command, const int factor, int floor[50][50]);
void draw(const int floor[50][50]);
// y and x coordinates of turtle
int posX =0;
int posY =0;

int main(void)
{
    //INSTRUCTIONS
    printf("Command Key: \n\n");
    printf("1 ---- pen up\n");
    printf("2 ---- pen down\n");
    printf("3 ---- turn right\n");
    printf("4 ---- turn left\n");
    printf("5, N ---- move forward N spaces\n");
    printf("6 ---- Print 50 by 50 Floor\n");
    printf("9 ---- End Data\n\n");
   
    printf("Enter your commands.\n");
    int floor[50][50] = {{0}, {0}};// floor of length and width of 50
    int commandArrays[100][2] = {{0}, {0}};// arrays for the commands
    int numberOfCommands = 0;
    
    numberOfCommands = copyToArrays(commandArrays, 100);// calculate how many commands to process
    
    //for loop to go through the commands and perform them one by one
    for(size_t i = 0; i < numberOfCommands; i++)
    {
        performCommand(commandArrays[i][0], commandArrays[i][1], floor);
    }
}


// function to copy commands into arrays and return the number of commands 
int copyToArrays(int commandArray[2][2], const int commands)
{
    int i;
    int arr[2];
    for ( i  = 0; i <commands && readCommand(arr); i++)
    {
        commandArray[i][0] = arr[0];
        commandArray[i][1] = arr[1];
    }
    
    return i;
}

// Function to take input commands from user
int readCommand(int commandCheck[])
{
    scanf("%d,%d", &commandCheck[0], &commandCheck[1]);
    
    if(commandCheck[0] != 5)
    {
        commandCheck[1] = 0;
    }
    
    if(commandCheck[0] == 9)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


// follow command and perform task accordingly
void performCommand(const int command, const int factor, int floor[50][50])
{
    int j; 
    switch(command)
    {
        case 1: pen = 1;
            break;
        case 2: pen = 0;
            floor[posX][posY] = 1;
            break;
        case 3: if (facing == 3)
                   {
                    facing = 6;
                   }
               else if (facing == 6)
               {
                facing  = 9;
               } 
               else if (facing == 9)
               {
                facing = 12;
               }
               else
               {
                facing  = 3;
               }
               break;
          case 4: if (facing == 3)
                   {
                    facing = 12;
                   }
               else if (facing == 12)
               {
                facing  = 9;
               } 
               else if (facing == 9)
               {
                facing = 6;
               }
               else
               {
                facing  = 3;
               }
               break;
           
           case 5: if (facing == 3)
                   {
                    for (j = 1; j <= factor; j++)
                    {
                        posX++;
                        if(pen == 0)
                        {
                            floor[posX][posY] = 1;
                        }
                    }
                    
                   }
                   else if (facing == 6)
                   {
                    for(j = 1; j <= factor; j++)
                    {
                        posY--;
                        if(pen == 0)
                        {
                            floor[posX][posY] = 1;
                        }
                    }
                   }
                   else if (facing == 9)
                   {
                    for(j = 1; j <= factor; j++)
                    {
                        posX--;
                        if(pen == 0)
                        {
                            floor[posX][posY] = 1;
                        }
                    }
                   }
                   else if(facing == 12)
                   {
                    for(j = 1; j <= factor; j++)
                    {
                        posY++;
                        if(pen == 0)
                        {
                            floor[posX][posY] = 1;
                        }
                    }
                   }
                   break;
            case 6: draw(floor);
                    break;
            default:
            break;
    }
}


//FUntion to draw on the floor
void draw(const int floor[50][50])
{
    
     printf("\n");
     for (size_t i = 0; i <=49; i++)
     {
        for(size_t j = 49; j >= 0; j--)
        {
            if(floor[i][j] == 1)
            {
                printf("%s", "*");
            }
            else
            {
                printf(" ");
            }
        }
        puts("");
     }
}

Upvotes: 0

Views: 68

Answers (1)

William Pursell
William Pursell

Reputation: 212298

This is wrong:

for(size_t j = 49; j >= 0; j--)

When j == 0, the loop is entered. When j is decremented, it does not become -1, since size_t is an unsigned type. Turn up your compiler warnings. When I compile your code, the compiler tells me:

a.c:181:30: warning: comparison of unsigned expression in '>= 0' is always true [-Wtype-limits]
  181 |         for(size_t j = 49; j >= 0; j--)

If your compiler doesn't tell you that, learn how to get that diagnostic. (eg, with gcc, always compile with -Wall -Wextra and pay attention to what it tells you.)

Upvotes: 1

Related Questions