I have an assignment to do for my Data Structures class and I'm in a little bit of trouble. I have some structures, namely:
typedef struct {
char *materie;
int ore_curs;
int ore_lab;
int credit;
int teme;
} TMaterie;
typedef struct {
char *nume;
float medie;
char grupa[6]; // 324CB + NULL
int varsta;
} TStudent;
typedef struct {
void *key;
void *value;
int frequency;
} Pair;
typedef struct celulag {
void *info;
struct celulag *urm;
} TCelulaG, *TLG, **ALG;
The last one is a generic list structure that will accept in the info any kind of structure. The thing is that I need to convert it to the structure pair so that it will point out to a key (that will be either a char or an int) and a value (that will be of the type TMaterie or TStudent). I need to make functions that allocate that accordingly and I have managed to do this:
TLG aloca_string_materie(char *key, TMaterie value){
TLG cel = (TLG) malloc(sizeof(TCelulaG));
if(!cel)
return NULL;
cel->info = (Pair*)malloc(sizeof(Pair));
if( !cel->info){
free(cel);
return NULL;
}
cel->urm = NULL;
((Pair*)cel->info)->value = (TMaterie*)malloc(sizeof(TMaterie));
((Pair*)cel->info)->key = (char*)malloc(50*sizeof(char));
((Pair*)cel->info)->frequency = 0;
((Pair*)cel->info)->key = key;
((TMaterie*)(Pair*)cel->info)->materie = value.materie;
((TMaterie*)(Pair*)cel->info)->ore_curs = value.ore_curs;
((TMaterie*)(Pair*)cel->info)->ore_lab = value.ore_lab;
((TMaterie*)(Pair*)cel->info)->credit = value.credit;
((TMaterie*)(Pair*)cel->info)->teme = value.teme;
return cel;
}
The problem that I am facing is that when I try to print out the key, namely
(((Pair*)cel->info)->key)
it gives me the same value as
((TMaterie*)(Pair*)cel->info)->materie
and I have no idea why.
Can someone please tell me what I'm doing wrong?