Thread: inserting strings into linked list nodes

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    17

    inserting strings into linked list nodes

    I've got a function which should malloc for a new node and then for space for a string in the data_item part of the node... but for the life of me I can't figure out why I get two errors... here is the function...


    Code:
    void insert_at_front(char *n, node_ptr list)
    {
            node_ptr new_node = (node_ptr) malloc(sizeof(struct node));
            new_node->data_item = (char *) malloc(sizeof(char) * strlen(n) + 1);
            strcpy(new_node->data_item, n); 
            new_node->next = list->next;
            list->next = new_node;
    }

    The two errors:

    -second malloc line error is: assignment makes integer from pointer without a cast

    -strcpy line error is: passing arg 1 of 'strcpy' makes pointer from integer without a cast

    If anyone can give me any clues this would be greatly appreciated as I've spent a lot of the day trying to nut it out with no real insight beyond knowing it is a casting issue...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to include the right header if you expect to use the functions described by said header.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    Hi, yes I think the header is alright but just to check...

    Code:
    #include "list.h"   /* is on the same page as the function */
    list.h contains:

    Code:
    typedef struct node *node_ptr;
    struct node
    {
        char data_item;
        node_ptr next;
    };
    
    /* Function to insert n at front of list */
    void insert_at_front(char *n, node_ptr list);
    This seems correct to me but obviously somewhere its going belly up... thanks for replying so fast btw...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, I mean to use malloc you have to include stdlib.h, and to use strcpy you have to include string.h.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    oh sorry... yes I have it in the top of the file with the function as the first malloc doesnt' throw an error...

    Code:
    #include <stdlib.h>

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    so basically at this point I can't really see why I'm getting those two cast errors on the second malloc and the strcpy functions...

    any suggestions?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. See the FAQ about casting malloc.

    2. Your data_item is a char (in other words, a small int - hence all the warnings) where it should be a char*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    thanks Salem, I'll read the FAQ and scratch my head and wonder why I never even thought of that one... I am pretty new at C though for sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swap nodes in linked list
    By Boaz in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 08:06 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM