0

I already get how to add an int to a linked list in C but I need to add a string and it simply doesn't work.

The main function gets the data from the user and prints it in the show function after adding it to the linked list.

list and main

struct nlista{
    char dado[10];
    struct nlista *prox;
}*Head;

int main(){
    int op;
    char data[10];
    Head = NULL;

    printf("type a value: ");
    scanf("%s",&data);
    inserir(data);
    printf("the element is : ");
    show();
}

inserir(): add the element in the end of list

    void inserir(char data){

    nlista *novoelemento;
    novoelemento = (struct nlista *)malloc(sizeof(struct nlista));
    nlista *check;
    check = (struct nlista *)malloc(sizeof(struct nlista));

    novoelemento->dado = data;

    if(Head == NULL){
        Head = novoelemento;
        Head->prox = NULL;
    }
    else{
        check = Head;
        while(check->prox != NULL)
            check = check->prox;

        check->prox = novoelemento;
        novoelemento->prox = NULL;  
    }

show(): display the linked list

    void show()
{
    nlista *check;
    check = (struct nlista *)malloc(sizeof(struct nlista));

    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->dado);
        check=check->prox;
    }
    printf("\n");
}

What am I missing? The compiler message is: invalid conversion from char* to char. in the line of inserir(data);

5
  • Why do you create a new element in this line: check = (struct nlista *)malloc(sizeof(struct nlista));? What purpose does that newly created element serve? And if inserir is supposed to add a string, why does it take a character parameter? It would help to have comments explaining why the code is written as it is. Commented Jun 15, 2019 at 0:47
  • data decays to a pointer (char *) but void inserir(char data) is asking for a char Commented Jun 15, 2019 at 0:50
  • @DavidSchwartz check will verify if the actual element of the list is null. the application is about add a linked list with multiple data types,but im stucked in add string so i simplify the code. so what kind of parameter should be? Commented Jun 15, 2019 at 1:10
  • @saulodev Probably whatever you want to use to represent a string. Most common is char*. Commented Jun 15, 2019 at 1:50
  • @davidschwartz don't work same problem Commented Jun 15, 2019 at 1:52

2 Answers 2

3

Sorry, but I found many mistakes in your code. I have written a very simple solution for your question. Kindly refer and correct your mistakes.

Value is nothing but, the string i.e data(in the code) passed as an argument in function inserir in the main function. Remember each node of the linked list consists of a string element and the pointer to the next node. The string can be of various length. step 1 and step 2 will take care of that(see the code). Hope you are clear now.

#include <stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct nlista{
    char *data;
    struct nlista *prox;
}Node;

Node * inserir(Node *, char *);
'''inserir function should return your head node'''
void show(Node *);
'''show function takes head node'''

int main()
{
    char data[10];
    Node * Head = NULL;

    printf("Type a value: ");
    scanf("%s",data);
    Head = inserir(Head,data);
    printf("the element is : ");
    show(Head);
}

Node* inserir(Node *Head, char *value)
{

    Node *novoelemento;
    novoelemento = (Node *)malloc(sizeof(Node));

    //step 1. allocate memory to hold word
     novoelemento->data = malloc(strlen(value)+1);

    //step 2. copy the current word
    strcpy(novoelemento->data,value);

    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = novoelemento;
        Head->prox = NULL;
    }
    else{   
        check = Head;
        while(check->prox != NULL)
            check = check->prox;

        check->prox = novoelemento;
        novoelemento->prox = NULL;  
    }
  return Head;    
}    
void show(Node *Head)
{
    //check is of Node type using which you traverse the list and print the values
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s", check->data);
        check=check->prox;
    }
    printf("\n");
}    

Hope this will help you.

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

4 Comments

While this isn't bad, my interpretation was he deliberately wants fixed-length strings. It's ok though. Let's just see which one he takes.
I understood his problem. I wanted to give him a more general answer. Thanks for pointing out that part.
Thank you very much Saurav, I have a question, the "char *value" in inserir can be shifted with char *data?
Yes, you can change in the function definition inserir. value is the name of the parameter I gave, which refers to the data only. Hope you are clear now.
2

We have char dado[10]; but novoelemento->dado = data; As you discovered, this does not compile.

You seem to want strncpy(novoelemento->dado, data, 10)[9] = 0; This copies the string within data over and ensures it's properly null terminated.

If you have strlcpy, you can do it better as strlcpy(novoelemento->dado, data, 10);

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.