简体   繁体   English

c中处理scanf的分段错误

[英]Segmentation fault in c dealing with scanf

I'm writing a program that requires me to do a union of two arrays. 我正在编写一个程序,要求我执行两个数组的并集。 Here is my code so far. 到目前为止,这是我的代码。 I get Segmentation fault as an error after I enter set A. 输入集合A后,我得到细分错误作为错误。

#include <stdio.h>

void Union(int a[], int b[], int set1, int set2)
{
    int u[20], i, j, unionIndex=0,trigger;

for(i=0; i<set1; i++)
{
    u[unionIndex] = a[i];
    unionIndex++;
}

for(i=0; i<set2; i++)
{
    trigger=0;
    for(j =0; j<set1; j++)
    {
        if(b[i] == u[j])
        {           
            trigger =1;
            break;
        }
    }
    if(trigger =0)
    {
        u[unionIndex]=b[i];
        unionIndex++;
    }
}

    for(i=0;i<unionIndex;unionIndex++)
    {
    printf(" %d",u[i]);
    }
}

   int main(void) {
   int N=0;
   int M=0;
   int i;
   int j;

   printf("Please enter the number of elements in set A: ");
   scanf("%d",N );
   int a[N];

   printf("Enter the numbers in set: ");
   for(i=0;i<N;i++)
   {
         scanf("%d",&a[i]);
   }

    printf("Please enter the number of elements in set B: ");
    scanf("%d",M );
    int b[M];

    printf("Enter the numbers in set: ");
    for(j=0;i<M;i++)
    {
         scanf("%d",&b[i]);
    }

    Union(a,b,N,M);
    return 0;
}

I'm pretty sure the issue has something to do with arrays because the program will compile but i get the error right after the user enters set A. I'm a beginner at C but I know a lot more about Java, so I'm thinking this has something to do with memory allocation. 我很确定问题与数组有关,因为该程序可以编译,但是在用户输入集合A之后我立即收到错误消息。我是C语言的初学者,但是我对Java有更多了解,所以我我认为这与内存分配有关。 I'm not really sure how to solve the issue, so if you could point me in the right direction that would be helpful. 我不太确定如何解决该问题,因此,如果您可以向我指出正确的方向,那将会有所帮助。

You need to pass the address of the variable to scanf() 您需要将变量的地址传递给scanf()

Change 更改

printf("Please enter the number of elements in set A: ");
scanf("%d",N );

to

printf("Please enter the number of elements in set A: ");
scanf("%d", &N);

Same goes for other place 其他地方也一样

printf("Please enter the number of elements in set B: ");
scanf("%d", &M);

There is another possible mistake 还有另一个可能的错误

Its here 它在这里

for(j =0; j<set1; j++)
{
    if(b[i] == u[j])

In this set1 is equal to N , so j will go from 0 to N-1 . 在这个set1中等于N ,因此j0 to N-1 And array u[] has only 20 elements. 数组u[]只有20元素。 There is a possibility of array access out of bound if some user enter value more then 20 for N . 如果某些用户为N输入的值大于20 ,则数组访问可能会超出范围。

The problem, as I see it is in 我认为问题出在

scanf("%d",N );

and

scanf("%d",M );

It invokes undefined behavior as scanf() needs the argument to a format specifier to be a pointer to the type. 它调用未定义的行为,因为scanf()需要格式说明符的参数作为指向该类型的指针。

Just to clarify, you're essentially passing the address as 0 (value of the variable), which is not a valid addres, anyway. 为了澄清起见,您实际上是将地址传递为0 (变量的值),无论如何,这不是有效的地址。

You need to pass the address there, like 您需要在此处传递地址,例如

scanf("%d", &N );

and

scanf("%d", &M );

That said, in your Union() function, you're using a user-defined value to limit the for loop, against a constant value 20 . 就是说,在Union()函数中,您使用用户定义的值将for循环限制for恒定值20 In case the user input is more than 20, you'll be overrunning the memory which invokes undefined behavior . 如果用户输入大于20,则将使调用未定义行为的内存溢出。

The reason you're getting the segmentation fault is because of how you're calling scanf when reading in N and M . 出现分段错误的原因是由于在读取NM时如何调用scanf The %d format specifier for scanf expects an int * , ie the address of an int, but you're passing in an int . scanf%d格式说明符期望一个int * ,即一个int的地址,但是您要传入一个int This is undefined behavior. 这是未定义的行为。

So you can fix them like this: 因此,您可以像这样修复它们:

 scanf("%d",&N );
 ....
 scanf("%d",&M );

Some addtional bugs: 一些其他错误:

When looping to read in the values for b : 循环读取b的值时:

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

You have the wrong loop indexes: 您有错误的循环索引:

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

When checking trigger : 检查trigger

if(trigger =0)

This is an assignment, not a comparison: 这是一项分配,而不是比较:

if(trigger == 0)

When looping to print out u : 循环打印出u

for(i=0;i<unionIndex;unionIndex++)

You're incrementing the wrong variable: 您正在递增错误的变量:

for(i=0;i<unionIndex;i++)

Finally, u need to have a length of at least set1 + set2 , otherwise you risk writing off the end of the array: 最后, u需要有至少长度set1 + set2 ,否则你可能会注销数组的结尾:

int u[set1+set2];

Fix those and you should get the desired results. 修复这些问题,您将获得期望的结果。

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

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