palapapa
palapapa

Reputation: 949

Can't Assign Value To Struct Array In Function

I've encountered a problem where my array of structs didn't get assigned with value when I assign them value in a function. Here are the struct declarations:

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;

And here are the functions:

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);
}

And when I use the "init_rooms()" function in the main function:

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

The printf only outputs a 0, and if I try to print out the "room_lobby.prop[0].texture", which is a char, it will only print a space when it should print a X. Thanks in advance!

Upvotes: 1

Views: 1254

Answers (2)

user3386109
user3386109

Reputation: 34829

When a structure is passed to a function, the function receives a copy of the structure, not a reference to the structure. So any changes the function makes only affect the copy, and not the original structure.

To make changes to the original structure, the caller needs to pass the address of the structure, and the function parameter needs to be a pointer to the structure.

So the code should look like this:

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);
}

Upvotes: 1

Yuanhui
Yuanhui

Reputation: 479

'struct parameter' is a value parameter. So the value will be copied into function, other then the variable. If you want change the value of a variable, you should use reference or address of struct parameter, like this:

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;
}

or

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;
}

Upvotes: 1

Related Questions