c - 如何解决“程序接收到的信号SIGSEGV,段错误”?

标签 c segmentation-fault

调试代码时,出现标题中提到的错误。我知道这不漂亮。直到第5部分正常工作。

我已经在没有第6部分的情况下对其进行了测试,并且可以正常工作。我要用第6部分做的是按顺时针螺旋顺序排列2D数组的打印元素,而不使用任何循环结构。

#include<stdio.h>

int check;
int arrprint(int a[20][20], int b[20][20], int n, int count, int check);
void loopprint(int a[20][20], int i, int n ,int choice1, int choice2, int x, 
int y);
void clockSpiral(int a[20][20], int n, int k, int l, int mc, int mr);

int main()
{
//PART 1

 int n,a[20][20];
 printf("\nEnter the order of the matrix(<=20) : ");
 scanf("%d",&n);
 int i,j;
 printf("\nEnter the elements of the matrix :\n");
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);
 printf("\nThe matrix in row major form is :\n");
 for(i=0;i<n;i++)
  { 
   for(j=0;j<n;j++)
    printf("%d ",a[i][j]);
   printf("\n"); 
  }

// PART 2

 printf("\nThe matrix in column major form is : ");
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   printf("%d ",a[j][i]);

 int c[20][20];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   c[i][j]=a[i][j];

 // PART 4

 printf("\nThe matrix in anti diagonal form is : ");
 int count=0;
 printf("\n");
 check=0;
 for(count=1;count<=n;count++)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }


 count=count-2;
 check=1;
 for(count;count>0;count--)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 } 

// PART 3

 int d[20][20];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   d[i][j]=a[i][j];

 printf("\nThe matrix in diagonal form is : ");
 printf("\n");
 check=3;
 for(count=1;count<=n;count++)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }

 count=count-2;
 check=4;
 for(count;count>0;count--)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }

//PART 5

 printf("\nThe matrix in anti clockwise spiral order is : ");
 count=0;
 int k=0,l=0,mr=n,mc=n;
 while(count<=n*n && k<mr && l<mc)
 {
  for(i=k;i<mr;i++)
  { 
   printf("%d ",a[i][l]);
   count++;
  }
  l++;
  for(i=l;i<mc;i++)
  {
   printf("%d ",a[mr-1][i]);
   count++;
  }
  mr--;
  for(i=mr-1;i>=k;i--)
  {
   printf("%d ",a[i][mc-1]);
   count++;
  }
  mc--;
  for(i=mc-1;i>=l;i--)
  {
   printf("%d ",a[k][i]);
   count++;
  }
  k++;
 }
 printf("\n");

//PART 6

 printf("\nThe matrix in clockwise spiral form is : ");
 clockSpiral(a,n,0,0,n,n);
 printf("\n");

 return 0;
}

int arrprint(int a[20][20], int b[20][20], int n, int count, int check)
{
 int i,j,ai,aj;
 if(check==0)
 {
  for(i=0,ai=0;ai<n && i<count;ai++,i++)
   {
    for(aj=0,j=0;aj<n && j<count;aj++,j++)
     {
      b[i][j]=a[i][j];
      if(i+j==count-1)
       printf("%d ",b[i][j]);
     }
   } 
 }

 if(check==1)
 {
  for(ai=n-count,i=0;ai<n && i<count;ai++,i++)
   { 
    for(aj=n-count,j=0;aj<n && j<count;aj++,j++)
    {
     b[i][j]=a[ai][aj];
     if(i+j==count-1)
      printf("%d ",b[i][j]);
    }  
   }
 }

 if(check==3)
 {
  for(aj=0,j=0;j<count && aj<n;j++,aj++)
   {
    for(ai=n-1,i=count-1; i>=0 && ai>=0;i--,ai--)
     {
      b[i][j]=a[ai][aj];
      if(i==j)
       printf("%d ",b[i][j]);
     }
   }
 }

 if(check==4)
{
 for(aj=n-count,j=0;j<count && aj<n;j++,aj++)
  {
    for(ai=0,i=0;ai<n && i<count;ai++,i++)
    {
     b[i][j]=a[ai][aj];
     if(i==j)
      printf("%d ",b[i][j]);
     }
   }
 } 

}


void loopprint(int a[20][20], int i, int n, int choice1, int choice2, int x, 
int y)
{
 switch(choice1)
 {
  case 1:
   if(choice2==1)
   {
    if(i<n)
    {
     y=i;
     printf("%d ",a[x][y]);
     i++;
     loopprint(a,i,n,1,1,x,i);
    }
   else 
     return;
   }
   if(choice2==2)
   {
    if(i<n)
    {
     x=i;
     printf("%d ",a[x][y]);
     loopprint(a,i,n,1,2,i,y);
    }
   else
     return;
   }
   break;
  case 2:
   if(choice2==1)
   {
    if(i<=n)
    {
     y=i;
     printf("%d ",a[x][y]);
     i--;
     loopprint(a,i,n,2,1,x,i);
    }
    else
     return;
   }
   if(choice2==2)
   {
    if(i>=n)
    {
     x=i;
     printf("%d ",a[x][y]);
     i--;
     loopprint(a,i,n,2,2,i,y);
    }
    else
     return;
   }
   break;
   default:
    return;
 }
 return;
}

void clockSpiral(int a[20][20], int n, int k, int l, int mc, int mr)
{
 if(k<mr && l<mc)
 {
  loopprint(a,l,mc,1,1,k,l);
  k++;
  loopprint(a,k,mr,1,2,k,mc-1);
  mc--;
  loopprint(a,mc-1,l,2,1,mr-1,mc-1);
  mr--;
  loopprint(a,mr-1,k,2,2,mr-1,l);
  l++;
  clockSpiral(a,n,k,l,mc,mr);
 }
 else
  return;
}

最佳答案

以下部分有一个错误,可能导致段冲突:

// PART 4

printf("\nThe matrix in anti diagonal form is : ");
int count=0;
printf("\n");
check=0;
for(count=1;count<=n;count++)
{
    int b[count][count];
    arrprint(a,b,n,count,check);
}


索引count上升到并包括n(C中的索引从0到n-1)。

函数arrprint的副作用是它实际上将count元素从a复制到b

如果n==20,则arrprinta的元素21复制到b的元素21。但是,这些元素不存在:seg错误!

另外,arrprint int a[20][20]的参数与带有a[count][count]的数组count!=20不同,因为第一个数组由20个整数组成,而第二个数组由<20个整数组成。因此寻址

b[i][j]=a[i][j];


将由编译器翻译为

b[i*20+j]=a[i*20+j];


这与

b[i*count+j]=a[i*count+j];


这也将导致段故障。

您应该将函数定义为:

int arrprint(int a[count][count], int b[count][count], int n, int count, int check)

关于c - 如何解决“程序接收到的信号SIGSEGV,段错误”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278053/

相关文章:

c - 为函数中的指针赋值时出现段错误

c - 取消引用自定义内存地址时出现段错误 (C)

c - 从/dev/ttyACM0 读取时出现段错误

c - 'ld' 无法链接符号,尽管它们在库中

php - 为什么 PHP-FPM 5.6 在 mysqlnd 调用期间出现段错误?

c++ - 删除链接器依赖项

c - 在 Linux 上分配期间如何记录堆栈?

c++ - 多线程类中互斥体可能出现段错误

c - eclipse C 编译器无法识别//行注释

c++ - 线程 - 同步和 sleep 线程拒绝唤醒(LINUX)