[英]How can I use struct parameter of a method to assign to another struct in C?
I am trying to insert values from the binary search tree to linkedlist.我正在尝试将二叉搜索树中的值插入到链表中。
struct node
{// Here node struct for leafs of the Tree
char word[STRING_LEN]; //words
struct node *left;
struct node *right;
};
struct nodeForLinkedList
{
char word[STRING_LEN]; //words
struct node *left;
struct node *right;
struct node *next;
}
void insertToLinkedList(nodeForLinkedList *nodeL,char *data){
struct nodeForLinkedList* new_node = (struct nodeForLinkedList*)malloc(sizeof(struct nodeForLinkedList));
strcpy(new_node->word, data); // To copy the elements of data to new node's word
new_node->next = *nodeL; // Error occurs here.**(1)**
*nodeL = new_node;// and here**(2)**
}
Errors:错误:
no suitable conversion function from "nodeForLinkedList" to "node *" existsC/C++(413) (1)
不存在从“nodeForLinkedList”到“node *”的合适转换 functionC/C++(413) (1)
no operator "=" matches these operands -- operand types are: nodeForLinkedList = nodeForLinkedList (2)
没有运算符“=”匹配这些操作数——操作数类型是:nodeForLinkedList = nodeForLinkedList (2)
You need to change the declaration of nodeL
to be a pointer to a pointer, so that you can update the caller's pointer variable.您需要将
nodeL
的声明更改为指向指针的指针,以便您可以更新调用者的指针变量。
void insertToLinkedList(nodeForLinkedList **nodeL,char *data){
struct nodeForLinkedList* new_node = (struct nodeForLinkedList*)malloc(sizeof(struct nodeForLinkedList));
strcpy(new_node->word, data); // To copy the elements of data to new node's word
new_node->next = *nodeL; // Error occurs here.**(1)**
*nodeL = new_node;// and here**(2)**
}
And when you call the function, it should be当您拨打 function 时,应该是
insertToLinkedList(&listVariable, stringVariable);
See Changing address contained by pointer using function for more information.有关详细信息,请参阅使用 function 更改指针包含的地址。
I am trying to insert values from the binary search tree to linkedlist.我正在尝试将二进制搜索树中的值插入到链表中。
struct node
{// Here node struct for leafs of the Tree
char word[STRING_LEN]; //words
struct node *left;
struct node *right;
};
struct nodeForLinkedList
{
char word[STRING_LEN]; //words
struct node *left;
struct node *right;
struct node *next;
}
void insertToLinkedList(nodeForLinkedList *nodeL,char *data){
struct nodeForLinkedList* new_node = (struct nodeForLinkedList*)malloc(sizeof(struct nodeForLinkedList));
strcpy(new_node->word, data); // To copy the elements of data to new node's word
new_node->next = *nodeL; // Error occurs here.**(1)**
*nodeL = new_node;// and here**(2)**
}
Errors:错误:
no suitable conversion function from "nodeForLinkedList" to "node *" existsC/C++(413) (1)
没有合适的转换 function 从“nodeForLinkedList”到“node *”存在C/C++(413) (1)
no operator "=" matches these operands -- operand types are: nodeForLinkedList = nodeForLinkedList (2)
没有运算符 "=" 匹配这些操作数 -- 操作数类型是: nodeForLinkedList = nodeForLinkedList (2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.