【问题标题】:Segmentation Fault: 11 C分段错误:11 C
【发布时间】:2015-03-12 22:00:47
【问题描述】:

我正在制作一个简单的命令行 Candy Crush 游戏。这是我下面的代码。我在 xCode 中编写它,然后使用 pico 在终端中运行它。游戏板打印正确,但没有在板上放置 Xs。而是打印电路板,然后发生分段错误。

//
// Assignment 3
//

#include <stdio.h>`enter code here`

//function headers
void displayOriginal (int img[10][10], int col, int row);

void removeOriginal (int img[10][10], int col, int row);

int main(){
  //declare variables and array
  int col = 0;
  int row = 0;
  int img[10][10];

  //call functions
  displayOriginal (img, col,row);    
  removeOriginal (img, col, row);        
  displayOriginal (img,col,row);

  return 0;
}

    //function for displaying gameboard
void displayOriginal (int img[10][10], int col, int row){   
  for(row = 0; row < 10; row++){//to create rows 0-9
    for(col = 0; col < 10; col++){//to create columns 0-9
      scanf("%d", &img[row][col]);
    }
  }
  printf("  0 1 2 3 4 5 6 7 8 9\n");//adds labels to x axis

  for(row = 0; row < 10; row++){
    printf("%d", row);//add labels to y axis

    for(col = 0; col < 10; col++){                  
      if (img[row][col] == 1){ //red
        printf("\x1b[41m  ");
      } else if (img[row][col] == 2){//green
        printf("\x1b[42m  ");
      } else if (img[row][col] == 3){//purple
        printf("\x1b[43m  ");
      } else if (img[row][col] == 4){//blue
        printf("\x1b[44m  ");
      } else if (img[row][col] == 5){//magenta
        printf("\x1b[45m  ");
      } else if (img[row][col] == 0){
        printf("XX");
      }
      printf("\x1b[m"); //white
    }
    printf("\n");            
  }
}

//function to label where the X's go
void removeOriginal (int img[10][10], int col, int row){
  //variables
  int previous = -10;
  int temp;
  int tally = 1;

  for(row = 0; row < 10; row++){
    for(col = 0; col < 10; col++){
      if (previous == img[row][col]){//if previous block =current then add 1
        tally++;
      } else {
        tally = 1;//return to 1 if previous does not equal current
      }

      if (tally >= 3){
        previous = img[row][col];
        for (temp = (tally-1); temp >= 0; temp--){
          img [row-temp][col] = 0;
        }
      } else {
        previous = img [row][col];
      }
      printf("Row %d Col %d Tally %d", row, col, tally);
    }
  }
}

【问题讨论】:

  • 多么独特的标题...
  • 欢迎来到 Stack Overflow。请在发布问题之前阅读此内容:stackoverflow.com/help/how-to-ask
  • 您应该使用调试器来运行您的程序以获取分段错误发生的位置(以及其他有用的信息,如变量值)

标签: c segmentation-fault


【解决方案1】:

您确定本节中的 row >= temp 吗?如果不是,img[-1][col] 将导致分段错误。

if (tally >= 3)
            {
                previous = img[row][col];
                for (temp = (tally-1); temp >= 0; temp--)
                {
                    img [row-temp][col] = 0;
                }
            }

【讨论】:

  • >我修改了代码,但仍然出现 seg 错误。这就是我将其更改为: if (tally >= 3) { previous = img[-1][col]; for (temp = (tally-1); temp >= 0; temp--) { img [row-temp][col] = 0; } }
  • 为什么?您的数组大小为 10。因此 img[1], img[2]... img [9] 将起作用。超出此范围的任何内容都将导致分段错误。
  • 我的意思是 img[0],img[1]...img[9]
  • 虽然在 C 中越界数组访问肯定总是未定义的行为,但如果您保持在一个映射页面。
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多