简体   繁体   English

如何在另一个结构指针中分配给一个结构指针

[英]How to assign to a struct pointer in another struct pointer

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 结构“ lastmove”现在更改为

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

Well, in your code, mover is a pointer to type p , which isn't defined anywhere - do you mean this? 好吧,在您的代码中, mover是指向p的指针,该指针未在任何地方定义-您是说这吗?

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 . 您需要为player*分配足够的空间player* ,这是指向玩家的指针,如果您需要的是足够的空间供player

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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