val is a char
, which is actually a number between 0 - 255.
What you want is a char *
, which is a string.
Frage
I want to add a string to a linked list in C. I was able to figure out how to add a integer, so I thought adding a string wouldn't be much different. So I tried the following:
struct node{
char val;
struct node * next;
};
typedef struct node item;
void linked_list(char letter[]) {
item * curr, * head;
int i;
head = NULL;
curr = (item *)malloc(sizeof(item));
curr->val = letter;
curr->next = head;
head = curr;
curr = head;
while(curr) {
printf("%s\n", curr->val);
curr = curr->next ;
}
}
However, I keep getting an
assignment makes integer from pointer without a cast
warning and
format '%s' expects type 'char *', but argument 2 has 'int'
If, in the struct, val is a character, why am I getting this error?
Side note: char letter[]
is passing in letters/characters from a separate main method.
I am learning about C and linked lists from this tutorial:http://www.learn-c.org/en/Linked_lists.
Lösung
val is a char
, which is actually a number between 0 - 255.
What you want is a char *
, which is a string.