Assign a pointer to a struct that is a member of a struct of the same type to another pointer to a struct of the same type

后端 未结 2 492
死守一世寂寞
死守一世寂寞 2021-01-24 11:01

This question sounds super confusing even for me, and it may seem obvious or already answered, but I have searched a lot and although I found interesting things, I didn\'t find

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-24 11:22

    In c++, when you say struct Node, Node [immediately] becomes a type. So, you could say:

    struct Node {
        char keyLine[100];
        int occurrences;
        Node *leftChild;
        Node *rightChild;
        Node *parent;
    };
    
    struct Tree {
        Node *root;
    };
    
    int
    insertNode(Tree *myTree, Node *newNode)
    {
        Node *currentNode = myTree->root;
        if (caseSenCmpString(newNode->keyLine, currentNode->keyLine) == -1) {
            currentNode = currentNode->leftChild;
        }
    }
    

    But, in c, it is merely in the "tag" namespace and does not define a type. Thus, you want:

    typedef struct Node {
        char keyLine[100];
        int occurrences;
        struct Node *leftChild;
        struct Node *rightChild;
        struct Node *parent;
    } Node;
    
    typedef struct Tree_struct {
        Node *root;
    } Tree;
    
    int
    insertNode(Tree *myTree, Node *newNode)
    {
        Node *currentNode = myTree->root;
        if (caseSenCmpString(newNode->keyLine, currentNode->keyLine) == -1) {
            currentNode = currentNode->leftChild;
        }
    }
    

    As an alternative, you can use a forward declaration:

    // forward declaration
    struct Node;
    typedef struct Node Node;
    
    struct Node {
        char keyLine[100];
        int occurrences;
        Node *leftChild;
        Node *rightChild;
        Node *parent;
    };
    
    typedef struct Tree_struct {
        Node *root;
    } Tree;
    
    int
    insertNode(Tree *myTree, Node *newNode)
    {
        Node *currentNode = myTree->root;
        if (caseSenCmpString(newNode->keyLine, currentNode->keyLine) == -1) {
            currentNode = currentNode->leftChild;
        }
    }
    

    Note that the struct name does not have to match the type name:

    // forward declaration
    struct Node_struct;
    typedef struct Node_struct Node;
    
    struct Node_struct {
        char keyLine[100];
        int occurrences;
        Node *leftChild;
        Node *rightChild;
        Node *parent;
    };
    
    typedef struct Tree_struct {
        Node *root;
    } Tree;
    
    int
    insertNode(Tree *myTree, Node *newNode)
    {
        Node *currentNode = myTree->root;
        if (caseSenCmpString(newNode->keyLine, currentNode->keyLine) == -1) {
            currentNode = currentNode->leftChild;
        }
    }
    

    To allow cross linking of your two structs, we could do:

    // forward declaration
    struct Node_struct;
    typedef struct Node_struct Node;
    
    struct Tree_struct;
    typedef struct Tree_struct Tree;
    
    struct Node_struct {
        char keyLine[100];
        int occurrences;
        Node *leftChild;
        Node *rightChild;
        Node *parent;
        Tree *tree;
    };
    
    struct Tree_struct {
        Node *root;
    };
    
    int
    insertNode(Tree *myTree, Node *newNode)
    {
        Node *currentNode = myTree->root;
        if (caseSenCmpString(newNode->keyLine, currentNode->keyLine) == -1) {
            currentNode = currentNode->leftChild;
        }
    }
    

提交回复
热议问题