0

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

  • "I am not allowed to use malloc"???!!!???!!!, use `realloc()` XD. – Stargateur Jan 24 '17 at 03:03
  • @Stargateur would memcpy or strcpy work? – T-bone's House Jan 24 '17 at 03:05
  • 2
    `char* dscrptn;` --> `char dscrptn[128];`(or more), `Room->dscrptn = tstr;` --> `strcpy(Room->dscrptn, tstr); ` – BLUEPIXY Jan 24 '17 at 03:05
  • @BLUEPIXY can i declare the size of the dscrptn inside of createRoom? Or do i have to allocate a seperate dscrpn array for each struct? – T-bone's House Jan 24 '17 at 03:08
  • In such a case use `malloc`.(`Room->dscrptn = strdup(tstr);` // `malloc` is used inside `strdup`. This need to release later by `free`.) – BLUEPIXY Jan 24 '17 at 03:12
  • @BLUEPIXY thank you very much i can give you answer if you write some answer. – T-bone's House Jan 24 '17 at 03:14
  • Can you give another example of what you exactly want? – RoadRunner Jan 24 '17 at 04:31
  • @RoadRunner not really able to give example, but I have an overall description updated to original post – T-bone's House Jan 24 '17 at 04:55
  • You should remove all code that is irrelevant to your question. Only show enough so that we know what you are doing. – Code-Apprentice Jan 24 '17 at 04:56
  • @Code-Apprentice removed irrelvant info, reformatted question – T-bone's House Jan 24 '17 at 05:03
  • 1
    These questions with silly restrictions like "I cannot use malloc" sound like homework, interview test Qs or such-like. *WHY* does anyone bother answering these... – Zan Lynx Jan 24 '17 at 05:04
  • @ZanLynx cause there are good people in the world – T-bone's House Jan 24 '17 at 05:06
  • Possible duplicate of [String pointer and array of chars in c](http://stackoverflow.com/questions/17077505/string-pointer-and-array-of-chars-in-c) – Zan Lynx Jan 24 '17 at 05:12
  • Not being able to use malloc is not that silly if you are working with embedded code. Many embedded libraries do not have malloc. You just have to have a different function that does the job of malloc like a global fixed sized array or some buddy algorithm routine. – cup Jan 24 '17 at 07:13
  • Sorry, but when you say _I have deleted irrelevant parts of main function_ you are destroying info that can be the place where you have made the mistake. Hiding irrelevant info (and more, using your supposedly erroneous criterion) can make your question completely useless. This is the reason for requiring a complete (this means compilable and executable) and verifiable example (this means that the example exposes, or gives the cited error) We request it to be minimum, because it's useless to say _My Word application has an error_ and try to post here all the Microsoft code base.... right? – Luis Colorado Jan 25 '17 at 08:34

0 Answers0