Thread: Linked list problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Linked list problem

    Hello, I am having this problem with a linked list. In a program I have created a method that links nodes together except when I check each node's data, all of them contain the same thing. This means that the lists is being created but not 100% correct. here is some of the code.
    The data that is being looked is a created struct called String that is defined as :
    Code:
    typedef struct {
           char *str; /* str is a dynamic array of characters*/
           int len;  /* number of characters */
        } String;
    here is the method that creates the linked list:
    Code:
    void addNode(listNode** headRef, String data) {   
      listNode* newNode = (listNode *)malloc(sizeof(listNode));
      newNode->data = data;
      newNode->next = *headRef;
      *headRef = newNode; 
    }

    Here is the main method:
    Code:
    String data;// data recieved from user
     listNode* list; // operation will be performed on this
     listNode* printer;// used to traverse through list
    
    int main(void) {
       
       data.str = (char*)malloc(sizeof(char)); //allocate memory
       printf("Enter a string:\n");
       scanf("%s", data);
       ListInitialize(list); //intitalize list
       addNode(&list, data); // add a node to the list
       printf("Enter another string:\n");
       scanf(" %s", data.str);
       addNode(&list,data);//add another node to the list
       printf("%d\n",Length(list));// check the length of list
       
       /** print each string in the list */
       for(printer = list; printer != NULL; printer = printer->next) {
           printf("%s\n", printer->data);
       }   
       freeList(list); 
       return 0;
    }
    When I run the code it looks like this:
    Enter a string:
    name1
    Enter another string:
    name2
    2
    name2
    name2

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    the struct data contains a str ,which is a pointer which is initialized to (char*)malloc(sizeof(char));
    u have to remember that str is a pointer,and so this doesnt contain the actual value,rather just the address of where a string array is to be found.
    so when u use scanf once, str is pointing to name1;
    and when u use scanf for another time, str is pointing to name 2;


    u have to remember its value has been updated to point to name2.
    so both link1.data and link2.data contain the address of str which in turn points to name2.
    Last edited by qqqqxxxx; 03-06-2006 at 12:15 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    yeah after looking at my code I realized my problem. Like you said it references. I just forgot to use the strcpy() function which fixes the problem thanks

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    sizeof(char)
    is always 1. It's guaranteed to always be 1.

    Code:
    scanf("%s", data);
    You might want to look into fgets() or other ways of getting input into a string. See the FAQ. [edit]Get a line of text from the user/keyboard (C)[/edit]

    And you shoudn't cast malloc(); see the FAQ again. [edit]Casting malloc[/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > data.str = (char*)malloc(sizeof(char));
    1. This only allocates a single char - which is actually useless for C-Strings which always end with a \0 (so you've no room for any actual data).
    2. Read the FAQ on why casting malloc is bad in C.

    > scanf("%s", data);
    Does your compiler even complain at this line?
    Or are you just ignoring all warnings?
    In any event, you just trashed something you shouldn't have, so the rest of the code becomes a moot point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM