Debuging a Struct

June 18, 2009 at 10:41:59
Specs: Xp, ?
I am trying to put a char array into a struct but everything I have tried has not worked.


typedef struct{
int Grid_X;
int Grid_Y;
char Room_Name[30];
int North;
int South;
int East;
int West;
int Warp;
int Warp_X;
int Warp_Y;
int Chest;

Room RG; //in main

char Name[30] = (*RG).Room_Name;
//In another function

void set_room_start(Room *RG){
(*RG).Grid_X = 0;
(*RG).Grid_Y = 0;
(*RG).Room_Name[30] = "Start Room";
(*RG).North = 1;
(*RG).South = 1;
(*RG).East = 1;
(*RG).West = 1;
(*RG).Warp = 1;
(*RG).Warp_X = 5;
(*RG).Warp_Y = 0;
(*RG).Chest = 1;


That gives me warnings:
"14 E:\Code\Source Code\Game Commands.c [Warning] assignment makes integer from pointer without a cast"

"71 E:\Code\Source Code\Game Commands.c array initialized from non-constant array expression"

Could some one point out the mistake I am making in my struct code? Thanx in advance

See More: Debuging a Struct

Report •

June 19, 2009 at 15:50:42
RMDan, you need to actually copy the char array into the struct.

In your main part, the code should read like this:
Room RG;
char Name[30];
strcpy(Name, RG.Room_Name);
This copies the struct Room_Name into Name.
Although your code doesn't show it, I assume RG is being populated somewhere.

According to your code RG is a struct, not a pointer to a struct, so *RG does not make sense.

If RG is a pointer to a struct, you can use (*RG).element but it looks better to use RG->element

The function void set_room_start(Room *RG) is more clear.
The line (*RG).Room_Name[30] = "Start Room"; should read
strcpy(RG->Room_Name, "Start Room");

Does that help?

Report •
Related Solutions

Ask Question