im having problem with allocating memory for my following structure:
typedef struct level {
char* raw_map; // original string representing the level map
char* name; // level name
char* description; // level description
char* password; // level password
struct level *next; // pointer to the next level
} LEVEL;
My function looks like this:
LEVEL* parse_level(char* line){
LEVEL *level =(LEVEL*)malloc(sizeof(LEVEL));
int i=0;
level->name = (char*)malloc(sizeof(level->name));
while(line[i] != ';'){
level->name[i]= line[i];
i++;
}
level->password =(char*)malloc(sizeof(level->password));
i++;
while(line[i] != ';'){
level->password[i]= line[i];
i++;
}
return level;
So far, i have only set first two types of structure.
I call funciton like that:
int main(){
LEVEL *first = (LEVEL*)malloc(sizeof(LEVEL));
first = parse_level("nameoffirstlevelwillbehere;mandragoramo5000");
printf("name: %s pass: %s\n",first->name, first->password);
}
Im not sure that issue somewhere in malloc structure because im using pointers for first time in functions so i'm not sure they are placed right. When i have code like that i will have in first->name only 12 characters instead of full string until first ';'. Same happened in level->password. Thanks for help.