The following insertSNode
function inserts item
and return
s updated pointer. Within the insertSnode
function, each data from different struct
is dereferenced accordingly.
PROBLEM: I get compiler errors on (LINE 1), (LINE 2), (LINE 3), (LINE 4) with the following error message:
"Member reference base type 'void' is not a structure or union."
QUESTION:
How do I get rid of compiler errors? If I can't, alternatively, do we have any better solution in writing functions as much identical as this situation? Let's assume there are too many struct
types (i.e. Type_A, Type_B, etc), and it is extremely inefficient to create different functions with different type declarations.
*pListTypeA = (Type_A *) insertSnode(*pListTypeA, pPreTypeA, pTypeAItem, TYPEA);
*pListTypeB = (Type_B *) insertSnode(*pListTypeB, pPreTypeB, pTypeBItem, TYPEB);
*pListTypeC = (Type_C *) insertSnode(*pListTypeC, pPreTypeC, pTypeCItem, TYPEC);
// more assignments
insertSnode
definition:
void* insertSnode(void* pList, void* pPre, char* item, const int type) {
void *pNew;
if (TYPEA == type) {
pList = (Type_A*) pList;
pPre = (Type_A*) pPre;
pNew = (Type_A*) pNew;
} else if (TYPEB == type) {
pList = (Type_B*) pList;
pPre = (Type_B*) pPre;
pNew = (Type_B*) pNew;
} else (TYPEC == type) {
pList = (Type_C*) pList;
pPre = (Type_C*) pPre;
pNew = (Type_C*) pNew;
}
if (!(pNew = malloc(sizeof(*pList)))) {
printf(ERR_NOT_ENOUGH_MEMORY);
exit(EXIT_NOT_ENOUGH_MEMORY);
}
pNew->name = item; // compiler error: (LINE 1)
if (pPre == NULL) {
pNew->link = pList; // compiler error: (LINE 2)
pList = pNew;
} else {
pNew->link = pPre->link; // compiler error: (LINE 3)
pPre->link = pNew; // compiler error: (LINE 4)
}
return pList;
}
NOTE:
FYI: I was able to run this code with no if-statements for type-declarations and just one type (e.g. Type_A). So, we all know there are no external problems than the type-declarations.