Pointers inside Struct

I have this code:

typedef struct s1{
    void *x;
} s1;

typedef struct s2{
    unsigned char y[5];
} s2;

s1 *s1_g;
s2 *s2_g;

What I wanted to do is to point pointer x to variable y.

I tried doing this:

s1_g->x = s2_g->y

but receives a segmentation error

Anyone can please enlighten me?


If that's all of the code, then you haven't even initialized s1_g and s2_g. So dereferencing them will no doubt cause a segmentation error.

There should be a valid object at the pointer location. Since, there is no valid object at s1_g, behaviour is undefined on dereferencing them.

Tags: C++ / C / Typedef

Similar questions

Assign char to array of chars inside an struct thats inside a array of structs thats inside a array of structs thats inside a array of struct
I need to simulate a hard disk for a college assignment. I have a char array inside a struct, another struct has an array of the last struct and so on. The teacher asked for it to be done this way. All of that in a declared as global. my problem is that i can't seem to access any of these. In my code, i use a loop to assign a variable to the "byte"...
How to access an element inside a struct that is inside another struct as a pointer?
I am trying to use SuperLU for a matrix inversion but I am unable to access the final result. It uses a few structures for the inversion and I know the answer is inside a structure but I can't reference it. B is defines as a supermatrix which has the format: Based on the Stype the structure of store changes. For B the struct used for *Store is: As ...
C Struct inside union inside struct
How do i initialize a variable of structure B or C? Something like A.u.B *bVar; doesn't work
int array inside a struct inside a struct
I am trying to achieve this... A single container which holds an array of structs and inside each of these structs are a single int array which represents an integer like so... 12,345 would be inside an int array[5]={1,2,3,4,5} What is a way that this could be implemented. My main problem with this implementation is I get lost with all the pointers...
Set a struct value inside a struct inside a vector given as void *
I am not able to set a vector value which is into a struct inside a struct inside a vector given as a void pointer. Is a bit complex. These are the definitions: I want to set the value of a sensor with data from a file. I'm using this: buffer is a void * correctly allocated as Attitude_t*1, in this case (but the 1 can vary), i has the value 0, and ...
Struct inside union inside struct in c
hello lets say i have this code i m doing a malloc for entry and now i want to copy a string to c from struct A . do i have to allocate memory for struct A and then for c or the first malloc for table entry allocates for all of them ? thank you in advance

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.