Thread: Problem with strings in Linked List

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    Problem with strings in Linked List

    I read a 26 page explanation online about linked lists from Stanford university, actually I read it about 100 times and I understood it in the end.

    So I copied the code and got it working fine. However, that tutorial and every tutorial online only looks at ints inside structures, it never looks at strings.

    I tried to modify the code to get it working with strings but I've got stuck.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	char strname[5];
    	struct node* next;
    };
    
    void Push (struct node** headRef, char* name);
    
    int main()
    {
    	
    		struct node* head = NULL;
    		
    		char name[5]="Paul";
    		Push(&head,name);
    		
    		struct node* current = head;
    		while (current !=NULL)
    		{
    			printf("%s", current->strname);
    			current = current->next;
    		
    		}
    }
    
    void Push(struct node** headRef, char* name)   
    {
    	
    	struct node* newNode = malloc(sizeof(struct node));   
    	
    	
    	newNode->strname= *name;			      
    	newNode->next = *headRef;			      
    	*headRef = newNode;				    
    	
    }
    The error I keep getting is about incompatible types in assignment
    Code:
    newNode->strname= *name
    . The only incompatibility is that one points to an array inside a structure and the other just points to an array.

    Now I suspect the problem is that unlike integers, you can't copy arrays as easy as a=b, and instead you have to use strcopy. But I don't know how to implement it here.

    Even so, I read my code as: create an array called name of 5 characters and assign it "Paul". Include the address of this array as an argument in the Push call function and in the called function point to it. Then, whatever name points to, which is "Paul", copy it to strname in the structure.

    Obviously there must be many things I am missing, which is why every tutorial just shows the simple linked list of integers only.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by hellogamesmaste View Post
    I read a 26 page explanation online about linked lists from Stanford university, actually I read it about 100 times and I understood it in the end.

    So I copied the code and got it working fine. However, that tutorial and every tutorial online only looks at ints inside structures, it never looks at strings.

    I tried to modify the code to get it working with strings but I've got stuck.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	char strname[5];
    	struct node* next;
    };
    
    void Push (struct node** headRef, char* name);
    
    int main()
    {
    	
    		struct node* head = NULL;
    		
    		char name[5]="Paul";
    		Push(&head,name);
    		
    		struct node* current = head;
    		while (current !=NULL)
    		{
    			printf("%s", current->strname);
    			current = current->next;
    		
    		}
    }
    
    void Push(struct node** headRef, char* name)   
    {
    	
    	struct node* newNode = malloc(sizeof(struct node));   
    	
    	
    	newNode->strname= *name;			      
    	newNode->next = *headRef;			      
    	*headRef = newNode;				    
    	
    }
    The error I keep getting is about incompatible types in assignment
    Code:
    newNode->strname= *name
    . The only incompatibility is that one points to an array inside a structure and the other just points to an array.

    Now I suspect the problem is that unlike integers, you can't copy arrays as easy as a=b, and instead you have to use strcopy. But I don't know how to implement it here.

    Even so, I read my code as: create an array called name of 5 characters and assign it "Paul". Include the address of this array as an argument in the Push call function and in the called function point to it. Then, whatever name points to, which is "Paul", copy it to strname in the structure.

    Obviously there must be many things I am missing, which is why every tutorial just shows the simple linked list of integers only.
    So the problem that you are facing is here

    char strname[5];

    It is not possible to assign the strname like the way you have done

    newNode->strname= *name

    So you can initialize the individual elements of an array but not the entire array at the same time.

    Another solution which i think should be good enough if you are planning to have a string variable as a data in the linked list would be to have a character pointer

    like char * strname;

    Initialize it to null in the beginning and then assign some value to it.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hellogamesmaste View Post
    Now I suspect the problem is that unlike integers, you can't copy arrays as easy as a=b, and instead you have to use strcopy. But I don't know how to implement it here.
    That's exactly it. You never have to implement strcpy; it's already been implemented for you by the implementation (clever eh). You just have to use it.
    Quote Originally Posted by hellogamesmaste View Post
    Even so, I read my code as: create an array called name of 5 characters and assign it "Paul". Include the address of this array as an argument in the Push call function and in the called function point to it. Then, whatever name points to, which is "Paul", copy it to strname in the structure.

    Obviously there must be many things I am missing, which is why every tutorial just shows the simple linked list of integers only.
    Nope, that's all you have to do.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    I got it working finally. I remembered that when you pass an address to an array it only looks at array 0, so you have to step through it using a loop to read and write each character.

    Code:
    int i;
    	for (i=0; i <=4; i++)newNode->strname[i]= name[i]
    I have never used strcpy before and I don't think it could be used for this could it?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I guess you can implement strcpy yourself if you want -- that's what you've done here, more or less.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    Lol I could have just done strcpy(newNode->strname,name); and that is it.

    In a tutorial they use strcopy, is that a typo on strcpy or something else?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Never heard of strcopy(), must be a typo.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    Ah I thought so.

    One thing I don't get with my program here is newNode->strname,name), in that usually you do *name to get the value of what the pointer points at.

    So usually you do:
    int a;
    int b;
    (1) int *p;
    (2) p=&a; - Point to address of variable a
    (3) b = *p - Point to value at variable a and assign it to variable b

    With my program I do 1 and 2 when passing the argument to the function, but it looks like, with newNode->strname,name), I am copying the address of name so strname shares the address. Which is like:
    (4) int *q
    q=p - Shared pointer, 2 pointers pointing to same address space.

    So the newNode->strname just points to the address it got from name and gets the values that way.

    When I tried newNode->strname,*name), it never worked. I tried to copy the value but it kept coming up with errors. Why is this?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you are thinking that pointers and arrays are more related than they are. char strname[5] is an array of chars. Arrays can not (at all) be assigned -- only the individual elements of the array can be assigned. (This is what strcpy does -- copies one character at a time from array B to array A.)

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    First off you're comparing apples to oranges. That is newNode->strname is a pointer to strname[0]. Similarly name is a pointer to name[0]. But *name dereferences name and is of type char. So your example above is the same as doing
    Code:
    int *p, *q;
    p = q   /* valid as they are both pointers */
    p = *q  /* invalid since *q is an integer while p is a pointer */

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    With your incorrect code above is it trying to store an int value as a pointer address? So instead of pointing to a memory address location, it's trying to point to a integer as a memory address location.

    The only possibilities between 2 pointers are:
    p=q /*point to same address as q */
    *p = *q /*Whatever value q points at, copy it and make p point at it */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  2. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 08:54 PM
  3. Linked List Problem
    By animeaholic in forum C Programming
    Replies: 1
    Last Post: 12-09-2002, 06:36 PM
  4. Linked List problem
    By spec85 in forum C Programming
    Replies: 1
    Last Post: 06-14-2002, 03:58 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM