1

Im having trouble assigning to struct pointer in another struct pointer

The structs looks like this:

header.h

typedef struct {
    int drawnlines;
    char *name;
} player;

typedef struct {
    char a, b;
    p *mover;
    p *previous;
} lastmove;

In another function Im calling another function that is supposed to change a pointer to this struct, this is also the function that gives me an warning:

program.c

#include header.h
.....
lastmove lmv;
lmv.mover=malloc(sizeof(player *));
lmv.previous=malloc(sizeof(player *));
player p;
p.drawnlines=0;
p.drawnlines=0;
strcpy(p.name, "patrik");
function(&lmv, &p);

.....
int
function(lastmove *lmv, player *p)
{
    lmv->a='b';
    lmv->b='a';
    lmv->previous=lmv->mover;
    lmv->mover=p;        // warning: assignment from incompatible pointer type
}

What is wrong in my code?

UPDATE: Now the code works, was a simple mistake. The struct "lastmove" is now changed to

typedef struct {
    char a, b;
    player *mover;
    player *previous;
} lastmove;
1
  • Do you want to use a cast like lmv->mover = (p *)p;,or you just mean p is 'player'? Commented Nov 12, 2012 at 2:15

1 Answer 1

1

Well, in your code, mover is a pointer to type p, which isn't defined anywhere - do you mean this?

typedef struct {
    char a, b;
    player *mover;
    player *previous;
} lastmove;

Also, I think there's an issue with your allocation. You're allocating enough space for a player*, which is a pointer to a player, when what you need is enough space for a player.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.