0

So I'm trying to understand how a linked list works with storing string type pieces of data. As far as I know, a linked list uses a data structure to store data in a somewhat fashionable way so you can easily enter new pieces of data inside, remove them, and rearrange them as you please. My problem is, I need to take a string in from the user, assign it a spot in the linked list and move on to the next node in the list and do the same thing again. At this point however, I'm simply trying to take one string from the user, store it in the new_node value, (or (*new_node).value for those of you thinking in terms of pointers and not linked lists) and print it out. The main just asks the user for a string input, the add_to_list func takes the input and adds it to the beginning of the linked list, and the print func simply prints what ever is in the linked list. Where the problem lies in my understanding (at least what I think is the problem) is the point at which I assign the data structure the value of the input, new_node->value=*n should just make the value contain the input string as it would just giving another array the value of whatever the pointer *n is containing, unfortunately that's not the case and I'm not sure why that is. Here's the code for simplicity's sake.

EDIT: Thanks everyone for the responses and the explanation behind why strcpy is necessary when dealing with assigning the value of an array of characters to another array ie: strings!

#include <stdio.h>
#include <stdlib.h>
#define LARGE 10

struct node *add_to_list(struct node *list, char *n);
struct node{
    char value[LARGE+1];
    struct node *next;
};

struct node *first = NULL;
void print(void);

int main(void) {
    
    char job[LARGE],*p;
    
    printf("Please enter printing jobs\n");
    scanf ("%s", job);
    p=&job[0];
    
    first = add_to_list(first, p);
    print();
    return 0;
}




struct node *add_to_list(struct node *list, char *n)
{
    struct node *new_node;
    new_node = malloc(sizeof(struct node));
    if (new_node == NULL) {
        printf("Error: malloc failed in add_to_list\n");
        exit(EXIT_FAILURE);
    }
    new_node->value = *n;
    new_node->next = list;
    return new_node;
}

void print(void){
    struct node *new_node;
    for(new_node=first;new_node!= NULL; new_node=new_node->next){
        printf("%s\n",new_node->value);
    }
}

6
  • Question or a story? Commented Dec 1, 2015 at 3:22
  • I guess I wasn't clear enough, the question is how would I assign the value the input string in the code new_node->value=*n. Why is that code incorrect? Commented Dec 1, 2015 at 3:28
  • @Senglish You cannot assign to an array like this . Use strcpy . strcpy(new_node->value,n); Commented Dec 1, 2015 at 3:41
  • That worked for the most part. Would you @ameyCU mind explaining why strcpy works instead of just doing what I had? EDIT: I am receive a warning pertaining to the usage of strcpy with (new_node->value,n) explaining that it's an implicit library function with type 'char *(char, *const char *). What's this trying to point out to me? The fact that I'm copying a pointer to a variable and not assigning the next node a value? Commented Dec 1, 2015 at 3:44
  • @Senglish Its just that you were assigning value directly to a array . But you are not allowed to do so .Therefore , this function strcpy exists for copying string . Commented Dec 1, 2015 at 3:47

2 Answers 2

3

Use strcpy instead of assigning a char to an array, which doesn't compile. Arrays are not lvalues, and cannot be assigned to without subscripting, so any assignment with an array name by itself on the left will not compile. Change

new_node->value = *n;

to

#include <string.h>

...

strcpy(new_node->value, n);
Sign up to request clarification or add additional context in comments.

Comments

1
  1. you cannot assign string as normal integer assignment.

  2. whenever you want to copy a array you have to use library functions like memcpy or strcpy. In your case its array of characters i.e string you have to use strcpy.

  3. usage char *strcpy(char *dest, const char *src);

so in your code it has to be like strcpy(new_node->value,n); instead of new_node->value=*n;

Reason for using strcpy is mentioned in the link below

why strcpy function

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.