0

I want to insert a string into struct data_node in a function called insert. My struct data_node is

struct data_node {
   char name [25];
   int data;
   struct data_node *next;
   };

and my insert function is:

struct data_node * insert (struct data_node **p_first, int elem, char *word ) {

    struct data_node *new_node, *prev, *current;
    current=*p_first;
    while (current != NULL && elem > current->data) {
       prev=current;
       current=current->next;
    } /* end while */

    /* current now points to position *before* which we need to insert */
    new_node = (struct data_node *) malloc(sizeof(struct data_node));
    new_node->data=elem;
    new_node->name=*word;

    new_node->next=current;
    if ( current == *p_first ) /* insert before 1st element */
       *p_first=new_node;
    else                       /* now insert before current */
       prev->next=new_node;
    /* end if current == *p_first */
    return new_node;
};

when I compile , it says that the line 22 incompatible types when assigning to type 'char[25]' from type 'char' that means new_node->name=*word; is wrong. how could I solve this problem?

0

3 Answers 3

1

The linked list structure is irrelevant. This problem boils down to copying one char[] to another. This will work:

strncpy(new_node->name, word, 25);

with some caveats. If word doesn't point to a valid char[], then this could cause undefined behavior. If it points to an array containing more than 25 25 or more (non-null) characters, the operation will copy the first 25 into new_node->name, which means that new_node->name will not be null-terminated, which could cause trouble later if other code assumes that it is. As WhozCraig points out, it's almost always a good idea to terminate the destination string with a null -- after being sure to leave room for it by copying one less character than you could (i.e. 25-1). And you might consider defining a constant NAMELENGTH so that you don't have the magic number 25 appearing here and there in your code.

Sign up to request clarification or add additional context in comments.

5 Comments

The no-termination caveat isn't just if the source string is longer than 25 non null chars, its at least 25 chars. Meaning a string exactly 25 chars long, not just longer than 25, will likewise not terminate. This is the reason when coding a truncation acceptance to use one-less than the target buffer length as the length parameter, and always hard-set the last char in the buffer to 0. If the string were shorter, the hard-set is benign (and in fact the entire tail of the buffer is already zeroed out anyway in that case).
@WhozCraig: 1) You're right, I was off-by-one. 2) You're slightly less right; setting the last char to 0, although not strictly necessary, is a good convention that I should have mentioned. I'll edit...
If you fail to terminate the string manually and the string being copied is N-length, where N is the third perimeter to strncpy(), it will not terminate the string. Setting the terminator isn't optional; its mandatory, particularly in the OP's case where the buffer is off a fresh malloc and thus filled with indeterminate data. Were the buffer off a calloc it would be optional, of course, but only if 25-1 (i.e. 24) or shorter is passed as the length.
@WhozCraig: I respectfully disagree. It will not null-terminate the string, but null-termination is not mandatory, just a good convention. As for the destination buffer starting out full of indeterminate data, it will wind up in the non-null-terminated state only if it has been completely overwritten with data from the source array, so I don't think we have to worry.
ok then, you're right. we will disagree, because without null-termination you don't have a C string, you have a char buffer. Do with that what you will, so long as nothing calls any C string function afterward that expects a termination char to function properly (which would be all of them). And the non-terminated case full-buff-case is precisely why we do worry.
0

data_node.data should be void *. This way you can store any type (though you will have to know what type it is when reading it).

Comments

0

You should use the strcpy method to copy word value in new_node -> name.

Take a look at this link.

strcpy(new_node->name, word);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.