1

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.

1
  • 1
    the struct defines a char not a char[] or char* Commented Mar 18, 2014 at 15:35

1 Answer 1

3

val is a char, which is actually a number between 0 - 255.

What you want is a char *, which is a string.

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

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.