I have multiple descriptions that I want to put into multiple roomInfo Structs. The way I am doing it makes it so that the description of the last room is the description for every room. I think this is because each of my rooms point to the same array in memory. Im wondering if there is a way to create a copy of the array and set it to Room.dscrptn?
struct roomInfo{
int rmNm;
char* dscrptn;
int nrth;
int sth;
int est;
int wst;
};
void getDescription(char* line, int* i,char* tstr){
//puts chars between [$,$] into tstr
//tstr[0] == '0' if error
int cash = 0, j = *i, t = 0;
while (cash < 2 && line[j] != '\0'){
if (line[j] == '$'){
++cash;
}
if (cash > 0){
tstr[t] = line[j];
++t;
}
++j;
}
tstr[t] = '\0';
if (tstr[0] == '$' && tstr[t-1] == '$'){
*i = j;
}
else{
tstr[0] = '0';
}
}
void createRoom(struct roomInfo* Room, char* line, char* tstr, int* numList, bool* p){
//try to create a room from information from line
getDescription(line, &i, tstr);
Room->dscrptn = tstr;
}
void createRooms(struct roomInfo* Rooms, char* tstr, int* numList, char* file){
//Creates an array of elements which contain info of the roooms
//Rooms[i] is the room i
unsigned int i = 0;
for (; i < TOTAL_ROOMS; i++){
struct roomInfo r;
Rooms[i] = r;
}
FILE* fp = fopen(file,"r");
char line[1000];
char* s;
while (fgets(line,sizeof(line), fp) != NULL){
i = strcspn(line, "\n");
line[i] = '\0';
struct roomInfo room;
createRoom(& room, line, tstr, numList, &p);
Rooms[room.rmNm-1] = room;
}
}
int main(){
struct roomInfo R[TOTAL_ROOMS];
char tstr[LINE_LENGTH];
int numbers[4] = {0, 0, 0, 0};
char dunginp[LINE_LENGTH];
char file[LINE_LENGTH];
createRooms(R, tstr, numbers, file);//somehow check to make sure some room was made
displayRoom(&R[cur]);
scanf("%s", dunginp);
}
EXMAPLE. So I have a main function that creates and array of 100 structs which represent rooms. I am going to parse a file where each line contains information about a room. To parse the file, I call createRooms() which takes the current line of text being iterated over, and attempts to parse it by calling create room. Create Room calls create description. My hope is that get description will give back a copy of the description to the struct. I have deleted irrelevant parts of main function