简体   繁体   English

c 中的结构和分段错误

[英]Struct and segmentation fault in c

I'm currently new to the struct and linked list.我目前是结构和链表的新手。 I'm trying to put the values of my array arr_dec[4] in a variable.我试图将我的数组 arr_dec[4] 的值放入一个变量中。 My array is on a struct, and also in my init function.我的数组在一个结构上,也在我的 init function 中。 I think my problem is on the display function, in my if loop.我认为我的问题是在我的 if 循环中显示 function。 Thanks for your help!谢谢你的帮助!

My struct我的结构

typedef struct Plane_dec Element; 
    struct Plane_dec 
    {
        int array_dec[4];  
        struct Plane_dec *head; 
        struct Plane_dec *next; 
    };

It's my initialisation function, i want to initialize my linked list.这是我的初始化 function,我想初始化我的链表。

void init(struct Plane_dec *plane_dec) 
    {
        plane_dec->head= NULL;
        plane_dec->next= NULL;
        int nbr_var= 0; 
        int arr_dec[4] = {2, 5, 1, 3}; 
    }

And my display function, in this one I want to test if my variable 'variable' takes the different values of the arr_dec[4].而我的显示器 function,在这个我想测试我的变量“变量”是否采用 arr_dec[4] 的不同值。

void display ()
    {
        int i = 0, variable = 0;
        struct Plane var;
        struct Plane_dec *plane_dec;
 
        init(&plane_dec);
 
        for(i = 0; i <4; i++) 
        { 
            variable = plane_dec->arr_dec[i]; 
            printf("%d - ", variable);
        }
    }

When I compile, I get those errors:当我编译时,我得到了这些错误:

plane.c: In function ‘display’:
plane.c:87:24: warning: passing argument 1 of ‘init’ from incompatible pointer type [-Wincompatible-pointer-types]
   87 |         init(&plane_dec);
      |                        ^~~~~~~~~~
      |                        |
      |                        struct Plane_dec **
plane.c:72:44: note: expected ‘struct Plane_dec *’ but argument is of type ‘struct Plane_dec **’
   72 |     void init(struct Plane_dec *Plane_dec) //init(Plane_dec);

Among other problems that prevent compilation:在其他阻止编译的问题中:
In display , you pass an uninitialized pointer to a pointer of Plane_dec (you declare a pointer and then pass its address) to init (which is stated to expect just a pointer,), and then within init you attempt to modify its fields.display中,您将未初始化的指针传递Plane_dec的指针(您声明一个指针,然后将其地址)传递给init (声明它只需要一个指针),然后在init中您尝试修改其字段。
If what you've been meaning to do is dynamically allocate a Plane_dec , then you need to actually do so: init needs to begin with a *plane_dec = malloc(...) , and you need to modify its declaration to accept a pointer to a pointer to Plane_dec , and not just a pointer to Plane_dec .如果您的意思是动态分配Plane_dec ,那么您需要实际这样做: init需要以*plane_dec = malloc(...)开头,并且您需要修改其声明以接受指针指向Plane_dec的指针,而不仅仅是指向Plane_dec的指针。
If, on the other hand, what you've been meaning to do is just modify a local variable declared within display and then discard it when display finishes its run, then you don't want display 's plane_dec to be a pointer (so remove the asterisk from the variable declaration).另一方面,如果您想做的只是修改在display中声明的局部变量,然后在display完成运行时将其丢弃,那么您不希望displayplane_dec成为指针(所以从变量声明中删除星号)。

Either way, the arr_dec you declare within init gets promptly discarded and doesn't have anything to do with the similarly-named field of the plane_dec variable, and nbr_var also doesn't do anything, as it is.无论哪种方式,您在init中声明的arr_dec都会被立即丢弃,并且与plane_dec变量的类似名称字段没有任何关系,并且nbr_var也不做任何事情,因为它是。 (Also note that in display you attempt to access the arr_dec field of Plane_dec , but the field is named array_dec , not arr_dec .) (另请注意,在display中,您尝试访问Plane_decarr_dec字段,但该字段名为array_dec ,而不是arr_dec 。)

If this doesn't solve your problem, please make sure to provide more context about what you've actually been meaning to do, as well as post your error (textually, not as an image), and create a minimal reproducible example : make sure to copy the code in question into a brand new file and test that it compiles, and provide the error details based on that .如果这不能解决您的问题,请确保提供更多关于您实际打算做什么的上下文,并发布您的错误(以文本形式,而不是作为图像),并创建一个最小的可重现示例:确保将有问题的代码复制到一个全新的文件中并测试它是否可以编译,并在此基础上提供错误详细信息。 It is quite likely that if you do all of this, you'll figure out the answers on your own;如果你做了所有这些,你很可能会自己找出答案。 and if you don't, we'll be able to help a lot more efficiently.如果您不这样做,我们将能够更有效地提供帮助。

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

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