user3079106
user3079106

Reputation: 1

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?

Upvotes: 0

Views: 51

Answers (2)

doptimusprime
doptimusprime

Reputation: 9395

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.

Upvotes: 0

Jim Buck
Jim Buck

Reputation: 20726

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.

Upvotes: 1

Related Questions