I have the following struct:
typedef struct ${
char author[27];
char iso[2];
int nrVolumes;
TVolume* volume; //this is another struct
}TAuthor;
I need a function to return a pointer to a TAuthor. This function will be passing an int-nrVol- and it has to return a pointer to TAuthor which has nrVolumes field = nrVol. I have made to functions.
TAuthor* aloc1(int nrVol){
TAuthor* new = (TAuthor*)malloc(sizeof(TAuthor));
new->nrVolumes = nrVol;
return new;
}
This one runs as expected.
TAuthor* aloc2(int nrVol){
char* new = malloc(sizeof(TAuthor));
(TAuthor*)new->nrVolumes = nrVol;
return (TAuthor*)new;
}
At compilation "aloc2" gives me this error : request for member 'nrVolumes' in something not a structure or union
Why doesn't my casting work ? Since "new" is just an array of bytes, I suppose that even by casting the compiler doesn't know which bytes are responsible for which field, but I am not sure of this