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

问题描述 投票:-1回答:2

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

这是代码:

int main(void)
{
        int *arr[SIZE][SIZE] = "\0";
        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 input\n");
                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;
}
c segmentation-fault
2个回答
0
投票

我在这里看到的问题很少

         int *arr[SIZE][SIZE] = "\0"; // 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];

0
投票
  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 input\n");
            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;
}
© www.soinside.com 2019 - 2024. All rights reserved.