Reputation: 3
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);
Upvotes: 0
Views: 5400
Reputation: 2357
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.
Upvotes: 3
Reputation: 43300
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);
Upvotes: 2