我需要弄清楚如何將兩個不同的結構體傳遞給函數。我嘗試使用無效*爲參數,但我收到的錯誤:對成員將不同的結構體傳遞給函數(使用void *)
warning: dereferencing 'void *' pointer
error: request for member left in something not a structure or union
同樣的錯誤,正確的
這是我籠統地做(代碼可能無法編譯)。
struct A{
char *a;
struct A *left, *right;
} *rootA;
struct B{
char *b;
struct B *left, *right;
} *rootB;
void BinaryTree(void *root, void *s){
if(condition)
root->left=s;
else if(condition)
BinaryTree(root->left, s);
if(condition)
root->right=s;
else if(condition)
BinaryTree(root->right, s);
}
int main(){
// Assume the struct of nodeA and nodeB get malloc()
// as well as the variables a and b with actual data.
struct A nodeA;
struct B nodeB;
BinaryTree(rootA, nodeA);
BinaryTree(rootB, nodeB);
return 0
}
你的代碼表明你沒有傳遞指針。你的意思是'BinaryTree(rootA,&nodeA)'等? – lurker
我正在傳遞nodeA和nodeB的內存位置。函數參數中的void指針應該將其解引用以用於函數no? – user2644819
您的參數類型是指針。你傳遞的是整個結構,而不是指向結構體的指針。儘管可能還有其他問題。編譯器不會將結構「解引用」爲void指針。 – lurker