C 什么是分段错误

C 什么是分段错误,c,segmentation-fault,C,Segmentation Fault,从1索引的零数组和操作列表开始,对于每个操作,在两个给定索引(包括)之间的数组元素中为每个元素添加一个值。执行所有操作后,返回数组中的最大值 我的代码是: #include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <std

从1索引的零数组和操作列表开始,对于每个操作,在两个给定索引(包括)之间的数组元素中为每个元素添加一个值。执行所有操作后,返回数组中的最大值

我的代码是:

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n,b;
    scanf("%d",&n);
    scanf("%d",&b);
    int arr[3][b];
    for(int i=0;i<b;i++)
    {
        for(int j=0;j<3;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    int man[n];
    for(int i=0;i<n;i++)
    man[i]=0;
    int start,end,change;
    for(int i=0;i<b;i++)
    {
        start=arr[i][0]-1;
        end=arr[i][1]-1;
        change=arr[i][2];
        for(int j=start;j<=end;j++)
        {
            man[j]=man[j]+change;
            //printf("%d",change);
        }
    }
    for(int i=0;i<n-1;i++)
    for(int j=0;j<n-i-1;j++)
    if(man[j]<man[j+1])
    {
        int temp=man[j+1];
        man[j+1]=man[j];
        man[j]=temp;
    }
    printf("%d",man[0]);
}
当n和b的值较低时,执行我的代码,但当值较高时,显示分段错误


有人能帮我吗?

至少这个问题是:数组维度错误

// int arr[3][b];
int arr[b][3];

for(int i=0;i<b;i++) {
    for(int j=0;j<3;j++) {
        scanf("%d",&arr[i][j]);
    }
}
//int-arr[3][b];
国际航空公司[b][3];

对于(int i=0;i如果您试图访问未正确分配的内存,则至少在您的情况下会发生分段错误。例如,您的
arr
在其第一维度中有3个整数的长度。因此,如果您输入b且大于2,则嵌套循环将在您到达
scanf(“%d”,&arr)时失败[大于或等于3的事物][j])
。可能您需要切换循环?或数组维度?

发布所用输入的确切文本。使用调试器查找导致segfault的行。并在该点显示相关变量值。有一个关于的维基百科页面。您阅读了吗?使用调用的
gcc-Wall-Wextra-g
然后使用调试器。阅读C编译器和debugger@chux-postedSO不是一个增量调试服务。您的编辑使答案无效,混淆了当前和未来的用户。回滚..即使这样也会得到相同的结果error@Baba_yaga在
man[j]=man[j]+change;
之前,添加
assert(j>=0&&j
测试那里的阵列访问。
// int arr[3][b];
int arr[b][3];

for(int i=0;i<b;i++) {
    for(int j=0;j<3;j++) {
        scanf("%d",&arr[i][j]);
    }
}