Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Different type declaration by casting void pointer in a function
    primarykey
    data
    text
    <p>The following <code>insertSNode</code> function inserts <code>item</code> and <code>return</code>s updated pointer. Within the <code>insertSnode</code> function, each data from different <code>struct</code> is dereferenced accordingly.</p> <p><strong>PROBLEM:</strong> I get compiler errors on <strong>(LINE 1), (LINE 2), (LINE 3), (LINE 4)</strong> with the following error message:</p> <blockquote> <p>"Member reference base type 'void' is not a structure or union."</p> </blockquote> <p><strong>QUESTION:</strong></p> <p>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 <code>struct</code> types (i.e. Type_A, Type_B, etc), and it is extremely inefficient to create different functions with different type declarations.</p> <pre><code>*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 </code></pre> <p><code>insertSnode</code> definition:</p> <pre><code>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-&gt;name = item; // compiler error: (LINE 1) if (pPre == NULL) { pNew-&gt;link = pList; // compiler error: (LINE 2) pList = pNew; } else { pNew-&gt;link = pPre-&gt;link; // compiler error: (LINE 3) pPre-&gt;link = pNew; // compiler error: (LINE 4) } return pList; } </code></pre> <p><strong>NOTE:</strong> </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload