采用多维数组元素时的分段错误

时间:2016-03-16 03:45:35

标签: c multidimensional-array segmentation-fault scanf

当我运行以下代码时出现错误;

  

编程接收信号SIGSEGV,分段故障。   来自/lib64/libc.so.6的__GI__IO_vfscanf()中的0x00007ffff7a74402   缺少单独的debuginfos,请使用:debuginfo-install   的glibc-2.17-78.el7.x86_64

我使用gcc warnings enable(-Wall)运行我的代码并逐步调试它。该错误在获取多维数组元素的行中。当我到达等级[1] [2]的步骤时,它表示无法访问地址0x00007ffff7a74402的内存。代码有什么问题?

 #include <stdio.h>

    //_________Prototypes________//

    int calcHighest(int grades[][4],int);
    int calcLowest(int grades[][4],int);
    int calcAverage(int grades[][4],int);

    int main (void){

      int funcSelection,nbOfStudent;
      int grades[nbOfStudent][4];
      int i,j;

      scanf("%d",&funcSelection);

     //check is one 1,2,3 and a number
      if(funcSelection <= 0 || funcSelection > 3){
        printf("Invalid Input.Please enter 1,2 or 3\n");
          return 1;
      }

      scanf("%d",&nbOfStudent);
     //check if it is positive and a number
      if(nbOfStudent <= 0){
        printf("Please enter a non-negative number\n");
         return 1;
      }

      for(i = 0;i < nbOfStudent;i++){
         for(j =0;j<4;j++){
           scanf("%d",&grades[i][j]);
         }
       }

      for(i = 0;i < nbOfStudent;i++){
         for(j =0;j<4;j++){
           scanf("%d",&grades[i][j]);
         }
       }

      if(funcSelection == 1){

        printf("%d\n",calcLowest(grades,nbOfStudent));
      }else if(funcSelection == 2){
        printf("%d\n",calcHighest(grades,nbOfStudent));
      }else{
        printf("%d\n",calcAverage(grades,nbOfStudent));
      }
      return 0;

    }

3 个答案:

答案 0 :(得分:4)

这就是问题所在:

int funcSelection,nbOfStudent;
int grades[nbOfStudent][4];

nbOfStudent定义时,请考虑grades的值是什么。由于nbOfStudent未初始化,因此任何东西都可能是它的值,并且使用未初始化的变量会调用Undefinedial Behavior。

通过移动

解决问题
int grades[nbOfStudent][4];

scanf("%d",&nbOfStudent);
//check if it is positive and a number
if(nbOfStudent <= 0){
  printf("Please enter a non-negative number\n");
 return 1;
}

答案 1 :(得分:2)

int funcSelection,nbOfStudent;
      int grades[nbOfStudent][4]; //problem leads to undefined behaviour
      int i,j;

nbofStudent不包含垃圾桶的任何值,导致未定义的行为。

解决方案:

在定义数组之前填充值nbofStudent。 在你的情况下scanfnbofStudent声明一个数组并检查变量的有效性。

scanf("%d",&nbOfStudent);
     //check if it is positive and a number
      if(nbOfStudent <= 0){
        printf("Please enter a non-negative number\n");
         return 1;
      }

应在这些说明之后定义变量。如此愚蠢地提到语句按顺序执行。

答案 2 :(得分:0)

发布的代码包含多个错误和疏忽,包括:

  1. 无法提示用户提供学生人数
  2. 包含魔法&#39;数字如3和4
  3. 未能提示用户为每位学生提供成绩
  4. 无法提示用户使用哪种功能&#39;待执行
  5. int用于只能为正的值
  6. 这是我建议的代码

    #include <stdio.h>
    #include <stdlib.h>  // exit(), EXIT_FAILURE
    
    #define NUM_GRADES (4)
    #define MAX_FUNC   (3)
    
    //_________Prototypes________//
    int calcHighest(int grades[][ NUM_GRADES ], size_t);
    int calcLowest( int grades[][ NUM_GRADES ], size_t);
    int calcAverage(int grades[][ NUM_GRADES ], size_t);
    
    int main (void)
    {
    
        printf( "Enter number of Students: ");
        size_t nbOfStudent;
        if( 1 != scanf("%lu",&nbOfStudent) )
        { // then scanf for number of students failed
            perror( "scanf for number students failed");
            exit( EXIT_FAILURE );
        }
    
        // implied else, scanf successful
    
        //only declare array 'grades[][]' after knowing number of students
        int grades[nbOfStudent][ NUM_GRADES ];
    
        printf( "Enter %d grades for each of the %lu students: ", NUM_GRADES, nbOfStudent);
        for(size_t i = 0;i < nbOfStudent;i++)
        {
            for(size_t j=0; j<NUM_GRADES; j++)
            {
                scanf("%d",&grades[i][j]);
            }
        }
    
    
        printf( "1: calculate lowest student grade\n");
        printf( "2: calculate highest student grade\n");
        printf( "3: calculate average student grade\n");
        printf( "Enter Selection: ");
        size_t funcSelection;
        scanf("%lu",&funcSelection);
        if( 1 != scanf("%lu",&funcSelection) )
        { // then scanf for function selection failed
            perror( "scanf for function selection failed");
            exit( EXIT_FAILURE );
        }
    
        // implied else, scanf successful
    
        switch( funcSelection )
        {
            case 1:
                printf("%d\n",calcLowest(grades,nbOfStudent));
                break;
    
            case 2:
                printf("%d\n",calcHighest(grades,nbOfStudent));
                break;
    
            case 3:
                printf("%d\n",calcAverage(grades,nbOfStudent));
                break;
    
            case 0: // fall through
            default:
                printf("Invalid Input.Please enter 1,2 or 3\n");
                break;
        } // end switch
    
        return 0;
    } // end function: main