I'm studying a double link list by myself, but I don't know why I used the strcpy function in the book example!

Asked 2 years ago, Updated 2 years ago, 97 views

I am preparing to return to school through a book called "Data Structure Written Easily in C Language."

I'm currently studying the double link list part.

I have a question because I don't understand something while looking at the example of the insertion operation!

// Define a dual connection node
typedef int element;
typedef struct DListNode {
    element data;
    struct DListNode* llink;
    struct DListNode* rlink;
}DListNode;

// Insertion operation
// Insert new data to the right of node before
void dinsert(DListNode *before, element data)
{
    DListNode *newnode = (DListNode *)malloc(sizeof(DListNode));
    **strcpy(newnode->data, data);**
    newnode->llink = before;
    newnode->rlink = before->rlink;
    before->rlink->llink = newnode;
    before->rlink = newnode;

}

In the example of the book, the int-type data seems to have been re-designated as element for uniformity and explanation.

I don't understand the star part here.

strcpy(newnode->data, data);

Although the data is int, he tried to substitute the factor value data into the data of the new node through strcpy.

Since strcpy() is char* strcpy(char*dest, const char*origin); I know that turning over the int-type data itself causes an error, and it actually does.

I understand that you are returning all the examples yourself so that there are no errors in the process of writing the book.

I wonder why you used the srtcpy() function, leaving the code newnode->data = data.

Thank you. :)

data-structure c

2022-09-20 16:39

1 Answers

It must be an error in the book error.


2022-09-20 16:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.