Domanda

I get an error when trying to assign a value to a pointer.

I have defined 2 structs:

typedef struct {
    struct player *next; //pointers
    struct player *prev;
} player;

typedef struct {
    player onMe; //object
} field;

later in the code, I create an instance of the struct "player p" and try to use it: fields[][] is an array that holds structs of the type field.

fields[p.x][p.y].onMe = *(p.next);
(*p.prev).next = &p.next;

in these cases i get "error: dereferencing pointer to incomplete type" I also tried (&p.next) but has the same result.

fields[x][y].onMe.prev = (&p);

in this case i get "warning: assignment from incompatible pointer type [enabled by default]"

Can someone tell me what I'm doing wrong?

È stato utile?

Soluzione

You don't declare struct player anywhere so the definition of the struct is incomplete. Try

typedef struct player_s {
    struct player_s *prev;
    struct player_s *next;
} player;

Altri suggerimenti

The reason is that struct player is incomplete type. You declare player type with typedef. But you've used struct player as its part, which is not defined now or anywhere else.

You should use something like

typedef struct player {
    struct player *next; //pointers
    struct player *prev;
} player;

It is possible that you assign another struct A pointer to the array.onMe one.
By the way, why you assign the ptr address to another ptr? May be you want assign the address a ptr pointered to to the other pointer, like below:

(*p.prev).next = p.next
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top