簡體   English   中英

如何使用方法的結構參數分配給 C 中的另一個結構?

[英]How can I use struct parameter of a method to assign to another struct in C?

我正在嘗試將二叉搜索樹中的值插入到鏈表中。

  • insertToLinkedList,我正在使用這個方法。 但是分配有問題。
  • 結構節點
  • 結構節點ForLinkedList
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)**


} 

錯誤:

不存在從“nodeForLinkedList”到“node *”的合適轉換 functionC/C++(413) (1)

沒有運算符“=”匹配這些操作數——操作數類型是:nodeForLinkedList = nodeForLinkedList (2)

您需要將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)**
}

當您撥打 function 時,應該是

insertToLinkedList(&listVariable, stringVariable);

有關詳細信息,請參閱使用 function 更改指針包含的地址

我正在嘗試將二進制搜索樹中的值插入到鏈表中。

  • insertToLinkedList,我正在使用這種方法。 但是分配有問題。
  • 結構節點
  • 結構節點ForLinkedList
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)**


} 

錯誤:

沒有合適的轉換 function 從“nodeForLinkedList”到“node *”存在C/C++(413) (1)

沒有運算符 "=" 匹配這些操作數 -- 操作數類型是: nodeForLinkedList = nodeForLinkedList (2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM