C语言 如何防止我的程序出现此分段错误(核心转储)



我有一个程序出现分段错误。该程序是一个魔方程序,要求用户输入正方形(矩阵)的大小,然后逐行询问正方形的数字。我使用指针指向 declareArray 函数中的数组,以引用它在主函数中声明的位置。我想保留指针以便练习使用它们,即使我知道我可以在没有指针的情况下使程序工作。我认为指针是这里的问题,但我找不到我做错了什么。

这是代码:

int main(void)
{
        int *arr[SIZE][SIZE] = "";
        declareArray(&arr);
        declareArray();
return 0;
}
        //main logic
int declareArray(int *arr[SIZE][SIZE])
{
        //variables
        int rowNumber = 0;
        int dimension = 0;
        //int arr[SIZE][SIZE] = {0};
        int row, col;
        int sum, sum1, sum2;
        int flag = 0;
        //ask for input of squares
        printf("Please enter the dimension of the square: ");
        //makes sure the size is between 1 and 15 for the dimension of the square
        if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
        {
                printf("invalid inputn");
                return 1;
        }
        //enter the data
        //array rows
        for(row = 0; row < dimension; ++row)
        {
                printf("Please enter the data for row %d: ", ++rowNumber);
                //array columns
                        for(col = 0; col < dimension; ++col)
                        {
                                //store the user input
                                scanf("%2d", &*arr[row][col]);
                        }
        }
        printf("n");
        printf("Here is the square");
        printf("n");
        //print the square
        //array rows
        for(row = 0; row < dimension; ++row)
        {
                //array columns
                for(col = 0; col < dimension; ++col)
                {
                        printf("%d", *arr[row][col]);
                }
                printf("n");
        }
 //Checks Sum of diagonal elements
   sum = 0;
   for (row = 0; row < dimension; row++) {
      for (col = 0; col < dimension; col++) {
         if (row == col)
            sum = sum + *arr[row][col];
      }
   }
   //Checks Sum of Rows
   for (row = 0; row < dimension; row++) {
      sum1 = 0;
      for (col = 0; col < dimension; col++) {
         sum1 = sum1 + *arr[row][col];
      }
      if (sum == sum1)
         flag = 1;
      else {
         flag = 0;
         break;
      }
   }
   //Checks sum of Columns
   for (row = 0; row < dimension; row++) {
      sum2 = 0;
      for (col = 0; col < dimension; col++) {
         sum2 = sum2 + *arr[col][row];
      }
      if (sum == sum2)
         flag = 1;
      else {
         flag = 0;
         break;
      }
   }
   //if the sums match, it will print a success message with the constant
   //if the sums dont match, a fail message will appear
   if (flag == 1)
      printf("nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d n", sum);
   else
      printf("nThe Square is not a magic square n");

return 0;
}

我在这里看到的问题很少

         int *arr[SIZE][SIZE] = ""; // this wont compile
         declareArray(&arr); // needless
         declareArray();
--- 
         int arr[SIZE][SIZE] = {};
         declareArray(arr);
         //declareArray();

声明函数

 int declareArray(int *arr[SIZE][SIZE]) // because function call changed
---
 int declareArray(int arr[SIZE][SIZE])

最后在printfscanf删除不再需要的*运算符

    scanf("%2d", &*arr[row][col]); // remove *
---
    scanf("%2d", &arr[row][col]);
    printf("%d", *arr[row][col]);  // remove *
---
    printf("%d", arr[row][col]);
    sum = sum + *arr[row][col];  // remove *
---
    sum = sum + arr[row][col];
  1. 请注意,当您声明数组时,数组的名称是指向数组的第一个元素的指针:

    &arr[0] == arr.
    
  2. 传递给 declareArray 函数的参数是指向整数的指针数组,因此分配了指针所需的空间,但实际整数的空间没有分配,因此当您尝试将整数扫描到 arr[row][col] 指向的地址时,您正在尝试写入它所持有的地址, 在您的情况下为 0,地址 0 很可能不在数据段中,因此segment_fault。

那你应该怎么做?

malloc() 分配所需的空间,将返回的地址分配给arr[row][col]然后scanf()如下所示,或者更简单更好地使用 int 数组并简单地将整数分配给arr[row][col]如上面的答案所示

#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
int declareArray(int * arr[SIZE][SIZE]);
int main(void)
{
    int * arr[SIZE][SIZE] = {0};
    declareArray(arr);
    return 0;
}
    //main logic
int declareArray(int * arr[SIZE][SIZE])
   {
    //variables
    int rowNumber = 0;
    int dimension = 0;
    int row, col;
    int sum, sum1, sum2;
    int flag = 0;
    int * myVal;
    //ask for input of squares
    printf("Please enter the dimension of the square: ");
    //makes sure the size is between 1 and 15 for the dimension of the square
    if(scanf("%d", &dimension) != 1 || dimension >= 15 || dimension < 1)
    {
            printf("invalid inputn");
            return 1;
    }
    //enter the data
    //array rows
    for(row = 0; row < dimension; ++row)
    {
            printf("Please enter the data for row %d: ", ++rowNumber);
            //array columns
                    for(col = 0; col < dimension; ++col)
                    {
                            printf("insert data to row %d col %d: ", rowNumber, col+1);                                
                            arr[row][col] = (int *) malloc(sizeof(int));
                            scanf("%2d", arr[row][col] );
                    }
    }
    printf("n");
    printf("Here is the square");
    printf("n");
    //print the square
    //array rows
    for(row = 0; row < dimension; ++row)
    {
            //array columns
            for(col = 0; col < dimension; ++col)
            {
                    printf("%d", *arr[row][col]);
            }
            printf("n");
    }
 //Checks Sum of diagonal elements
   sum = 0;
   for (row = 0; row < dimension; row++) {
      for (col = 0; col < dimension; col++) {
        if (row == col)
            sum = sum + *arr[row][col];
      }
   }
      //Checks Sum of Rows
      for (row = 0; row < dimension; row++) {
      sum1 = 0;
      for (col = 0; col < dimension; col++) {
         sum1 = sum1 + *arr[row][col];
      }
      if (sum == sum1)
         flag = 1;
      else {
         flag = 0;
         break;
      }
   }
   //Checks sum of Columns
   for (row = 0; row < dimension; row++) {
      sum2 = 0;
      for (col = 0; col < dimension; col++) {
         sum2 = sum2 + *arr[col][row];
      }
      if (sum == sum2)
         flag = 1;
      else {
         flag = 0;
         break;
      }
    }
   //if the sums match, it will print a success message with the constant
   //if the sums dont match, a fail message will appear
   if (flag == 1)
      printf("nTHE SQUARE IS A MAGIC SQUARE WITH A CONSTANT OF %d n", sum);
   else
      printf("nThe Square is not a magic square n");
 return 0;
}

相关内容

  • 没有找到相关文章

最新更新