so the topic is dynamic allocation.
this is the struct im working with
and this is the code that uses it
so... yeah. salem, i know youve told me something about the typecasting way i do malloc()'s, but obviously i didnt listen. ^^;; sorry.
the problem is this: it crashes when sprintf()'ing "Mountains" and prints jibberish instead of "Plains". yet "Forest" works just fine.
foolishness, im sure. however, im self taught and the one person i know in this town thats better than i am is across the continent. so i have no idea where to turn aside from the forums.
žÅNžÅ
this is the struct im working with
Code:
typedef struct
{
int RegionCount, NameLength;
char *name;
struct
{
int TownCount, NameLength;
char *name;
}*region;
}*world;
Code:
world gia = (world) malloc(sizeof(gia));
gia->NameLength = 4;
gia->name = (char *) malloc(sizeof(char) * gia->NameLength);
sprintf(gia->name, "Gia");
gia->RegionCount = 3;
gia->region = malloc(sizeof(gia->region) * gia->RegionCount);
gia->region[0].NameLength = 7;
gia->region[0].name = (char *) malloc(sizeof(char) * gia->region[0].NameLength);
sprintf(gia->region[0].name, "Plains");
gia->region[0].NameLength = 7;
gia->region[1].name = (char *) malloc(sizeof(char) * gia->region[1].NameLength);
sprintf(gia->region[1].name, "Forest");
gia->region[0].NameLength = 10;
gia->region[2].name = (char *) malloc(sizeof(char) * gia->region[2].NameLength);
sprintf(gia->region[2].name, "Mountains");
so... yeah. salem, i know youve told me something about the typecasting way i do malloc()'s, but obviously i didnt listen. ^^;; sorry.
the problem is this: it crashes when sprintf()'ing "Mountains" and prints jibberish instead of "Plains". yet "Forest" works just fine.
foolishness, im sure. however, im self taught and the one person i know in this town thats better than i am is across the continent. so i have no idea where to turn aside from the forums.
žÅNžÅ