如何修复显示 C 语言分段错误的代码?

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

对于我的作业,我试图在 C 中重新创建海龟图形。基本上,当您输入一系列命令时,我的程序会处理这些命令并相应地在 50 x 50 阵列(地板)中绘制一个形状。当我 运行 我的程序时,它一直给我分段错误,我不知道那是什么意思,所以我无法发现我的错误。

这是我的代码:


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("");
     }
}

这是错误的:

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

当j == 0时,进入循环。当 j 递减时,它不会变为 -1,因为 size_t 是无符号类型。打开你的编译器警告。当我编译你的代码时,编译器告诉我:

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--)

如果您的编译器没有告诉您,请了解如何获取该诊断信息。 (例如,使用 gcc,始终使用 -Wall -Wextra 进行编译并注意它告诉您的内容。)