Can';t将值赋给函数中的结构数组

Can';t将值赋给函数中的结构数组,c,function,struct,C,Function,Struct,我遇到了一个问题,当我在函数中为结构数组赋值时,它没有被赋值。 以下是结构声明: typedef struct{ int length; int width; char texture; int xpos; int ypos; }prop_info; typedef struct{ int init_xpos; int init_ypos; int index; prop_info prop[100]; }room_info

我遇到了一个问题,当我在函数中为结构数组赋值时,它没有被赋值。 以下是结构声明:

typedef struct{
    int length;
    int width;
    char texture;
    int xpos;
    int ypos;
}prop_info;
typedef struct{
    int init_xpos;
    int init_ypos;
    int index;
    prop_info prop[100];
}room_info;
以下是功能:

void info_setup(room_info room,int init_xpos,int init_ypos,int index)
{
    room.init_xpos=init_xpos;
    room.init_ypos=init_ypos;
    room.index=index;
}
void prop_setup(room_info room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room.prop[prop_index].length=length;
    room.prop[prop_index].width=width;
    room.prop[prop_index].texture=texture;
    room.prop[prop_index].xpos=xpos;
    room.prop[prop_index].ypos=ypos;
}
room_info room_lobby;
void init_rooms()
{
    info_setup(room_lobby,0,0,0);
    prop_setup(room_lobby,0,1,1,'X',5,5);
}
当我在主函数中使用“init_rooms()”函数时:

int main()
{
    init_rooms();
    printf("%d",room_lobby.prop[0].xpos);
}
printf只输出一个0,如果我试图打印出“room_lobb.prop[0].texture”,这是一个字符,它只会在打印X时打印一个空格。
提前谢谢

“结构参数”是一个值参数。因此,值将被复制到函数中,而不是变量中。如果要更改变量的值, 您应该使用结构参数的引用或地址,如下所示:

void info_setup(room_info& room,int init_xpos,int init_ypos,int index)
{
    room.init_xpos=init_xpos;
    room.init_ypos=init_ypos;
    room.index=index;
}
void prop_setup(room_info& room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room.prop[prop_index].length=length;
    room.prop[prop_index].width=width;
    room.prop[prop_index].texture=texture;
    room.prop[prop_index].xpos=xpos;
    room.prop[prop_index].ypos=ypos;
}
void info_setup(room_info *room,int init_xpos,int init_ypos,int index)
{                      // ^--- declare room as a pointer
    room->init_xpos=init_xpos;
    room->init_ypos=init_ypos;
    room->index=index;
     // ^^--- access the structure members using pointer notation
}
void prop_setup(room_info *room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room->prop[prop_index].length=length;
    room->prop[prop_index].width=width;
    room->prop[prop_index].texture=texture;
    room->prop[prop_index].xpos=xpos;
    room->prop[prop_index].ypos=ypos;
}
room_info room_lobby;
void init_rooms(void)
{
    info_setup(&room_lobby,0,0,0);
    prop_setup(&room_lobby,0,1,1,'X',5,5);
           //  ^--- pass the address of the structure
}

int main(void)
{
    init_rooms();
    printf("%d\n",room_lobby.prop[0].xpos);
}


“结构参数”是一个值参数。因此,值将被复制到函数中,而不是变量中。如果要更改变量的值, 您应该使用结构参数的引用或地址,如下所示:

void info_setup(room_info& room,int init_xpos,int init_ypos,int index)
{
    room.init_xpos=init_xpos;
    room.init_ypos=init_ypos;
    room.index=index;
}
void prop_setup(room_info& room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room.prop[prop_index].length=length;
    room.prop[prop_index].width=width;
    room.prop[prop_index].texture=texture;
    room.prop[prop_index].xpos=xpos;
    room.prop[prop_index].ypos=ypos;
}
void info_setup(room_info *room,int init_xpos,int init_ypos,int index)
{                      // ^--- declare room as a pointer
    room->init_xpos=init_xpos;
    room->init_ypos=init_ypos;
    room->index=index;
     // ^^--- access the structure members using pointer notation
}
void prop_setup(room_info *room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room->prop[prop_index].length=length;
    room->prop[prop_index].width=width;
    room->prop[prop_index].texture=texture;
    room->prop[prop_index].xpos=xpos;
    room->prop[prop_index].ypos=ypos;
}
room_info room_lobby;
void init_rooms(void)
{
    info_setup(&room_lobby,0,0,0);
    prop_setup(&room_lobby,0,1,1,'X',5,5);
           //  ^--- pass the address of the structure
}

int main(void)
{
    init_rooms();
    printf("%d\n",room_lobby.prop[0].xpos);
}


将结构传递给函数时,函数接收结构的副本,而不是对结构的引用。因此,函数所做的任何更改只会影响副本,而不会影响原始结构

要更改原始结构,调用方需要传递结构的地址,函数参数需要是指向结构的指针

因此,代码应该如下所示:

void info_setup(room_info& room,int init_xpos,int init_ypos,int index)
{
    room.init_xpos=init_xpos;
    room.init_ypos=init_ypos;
    room.index=index;
}
void prop_setup(room_info& room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room.prop[prop_index].length=length;
    room.prop[prop_index].width=width;
    room.prop[prop_index].texture=texture;
    room.prop[prop_index].xpos=xpos;
    room.prop[prop_index].ypos=ypos;
}
void info_setup(room_info *room,int init_xpos,int init_ypos,int index)
{                      // ^--- declare room as a pointer
    room->init_xpos=init_xpos;
    room->init_ypos=init_ypos;
    room->index=index;
     // ^^--- access the structure members using pointer notation
}
void prop_setup(room_info *room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room->prop[prop_index].length=length;
    room->prop[prop_index].width=width;
    room->prop[prop_index].texture=texture;
    room->prop[prop_index].xpos=xpos;
    room->prop[prop_index].ypos=ypos;
}
room_info room_lobby;
void init_rooms(void)
{
    info_setup(&room_lobby,0,0,0);
    prop_setup(&room_lobby,0,1,1,'X',5,5);
           //  ^--- pass the address of the structure
}

int main(void)
{
    init_rooms();
    printf("%d\n",room_lobby.prop[0].xpos);
}

将结构传递给函数时,函数接收结构的副本,而不是对结构的引用。因此,函数所做的任何更改只会影响副本,而不会影响原始结构

要更改原始结构,调用方需要传递结构的地址,函数参数需要是指向结构的指针

因此,代码应该如下所示:

void info_setup(room_info& room,int init_xpos,int init_ypos,int index)
{
    room.init_xpos=init_xpos;
    room.init_ypos=init_ypos;
    room.index=index;
}
void prop_setup(room_info& room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room.prop[prop_index].length=length;
    room.prop[prop_index].width=width;
    room.prop[prop_index].texture=texture;
    room.prop[prop_index].xpos=xpos;
    room.prop[prop_index].ypos=ypos;
}
void info_setup(room_info *room,int init_xpos,int init_ypos,int index)
{                      // ^--- declare room as a pointer
    room->init_xpos=init_xpos;
    room->init_ypos=init_ypos;
    room->index=index;
     // ^^--- access the structure members using pointer notation
}
void prop_setup(room_info *room,int prop_index,int length,int width,char texture,int xpos,int ypos)
{
    room->prop[prop_index].length=length;
    room->prop[prop_index].width=width;
    room->prop[prop_index].texture=texture;
    room->prop[prop_index].xpos=xpos;
    room->prop[prop_index].ypos=ypos;
}
room_info room_lobby;
void init_rooms(void)
{
    info_setup(&room_lobby,0,0,0);
    prop_setup(&room_lobby,0,1,1,'X',5,5);
           //  ^--- pass the address of the structure
}

int main(void)
{
    init_rooms();
    printf("%d\n",room_lobby.prop[0].xpos);
}