简体   繁体   English

C程序中的分段错误

[英]Segmentation error in c program

I am getting segmentation error when trying to do this simple c sorting program I am a novice in C language . 尝试执行此简单的c排序程序时遇到分段错误,我是C语言的新手。 And can you please explain me why i am getting segmentation error 能否请您解释一下我为什么会出现细分错误

#include<stdio.h>

int main(void)
{
    int prev,next,result,total_number;
    int i,j=1,b;
    int a[i];

    printf("Number of values to be entered");
    scanf("%d",total_number);
    printf(" enter the values \n");
    for(i=0;i<=total_number-1;i++)
    {
        printf(" enter the values \n");
        scanf("%d",a[i]);
    }
    for(i=0;i<=total_number-2;i++)
    {
        for(j=1;j<=total_number-1;j++)
        {
            if(a[i]>a[j])
            {
                b=a[i];
                a[i]=a[j];
                a[j]=b;
            }
            else
            {
                break;
            }
        }
    }
    for(i=0;i<total_number-1;i++)
    {
        printf("The numbers are %d",a[i]);
    }

}

The segmentation fault is caused by this line: 分段错误是由以下行引起的:

scanf("%d",total_number);

You're missing the address of (&) operator, it should be like this: 您缺少(&)运算符的地址,应该是这样的:

scanf("%d",&total_number);

The operator is also missing in this line: 此行中也缺少运算符:

    scanf("%d",a[i]);

You can find more details on scanf in the glibc manual : 您可以在glibc手册中找到有关scanf的更多详细信息:

Another area of difference between scanf and printf is that you must remember to supply pointers rather than immediate values as the optional arguments to scanf; scanf和printf之间的另一个区别是,您必须记住要提供指针而不是立即值作为scanf的可选参数。 the values that are read are stored in the objects that the pointers point to. 读取的值存储在指针指向的对象中。 Even experienced programmers tend to forget this occasionally, so if your program is getting strange errors that seem to be related to scanf, you might want to double-check this. 即使是经验丰富的程序员也会偶尔忘记这一点,因此,如果您的程序遇到与scanf有关的奇怪错误,则可能需要仔细检查。

But there are other subtle errors in your code, too: the fact the int i in array[i] is not initialized leads to undefined behaviour ie, anything could happen. 但是代码中也存在其他细微的错误:array [i]中的int i未被初始化的事实会导致未定义的行为,即,可能发生任何事情。 You can use malloc to allocate space for the array, but a simple reordering could be enough: 您可以使用malloc为数组分配空间,但是简单的重新排序就足够了:

scanf("%d",&total_number);
int a[total_number];

Uses user input to allocate the array. 使用用户输入来分配数组。

Lastly, it seems like you're trying to implement an insertion sort algorithm, but the logic is slightly flawed: even correcting the bugs in the code the input 最后,似乎您正在尝试实现一个插入排序算法,但是逻辑上有一点缺陷:甚至纠正输入中代码中的错误

1 3 5 0

gets "ordered" to 被“命令”到

1 5 3 0

But I don't know what you were trying to implement. 但是我不知道您要实现什么。 In case you were actually trying to implement insertion sort, you could use the pseucode from the insertion sort wiki article to get an idea of what's missing in your implementation. 如果您实际上是在尝试实现插入排序,则可以使用插入排序 Wiki文章中的pseucode来了解实现中缺少的内容。

int a[i]; is creating an array of arbitrary size since i is not initialized. 正在创建任意大小的数组,因为我尚未初始化。

Instead, you can dynamically create the equivalent of an array of int once you know total_number. 相反,一旦知道total_number,就可以动态创建一个与int数组等效的数组。

int* a;

. . . snip . . .

/* once you know total_number */

a = (int*) malloc(total_number, sizeof(int));

/* you can use a with array notation as long as you stay in bounds */

a[i] = something;

/* don't forget to free a when you are done */

free(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM