简体   繁体   中英

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. My array is on a struct, and also in my init function. I think my problem is on the display function, in my if loop. 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.

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].

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.
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 .
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).

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. (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 .)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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