Thread: Unable to stop at the end of linked list

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Unable to stop at the end of linked list

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct list1{
    	char *name;
    	struct list1 *next;
    };
    
    struct list1* freemem1(struct list1 **node)
    {
    	struct list1 *temp;
    	temp=*node;
    	if(temp==NULL)
    	{ 
    		temp=temp->next;
    		free(*node);
    	}
    	return temp;
    }
    
    void addname(char name[],struct list1 **node)
    {
    	struct list1 *x,*ptr;
    	char *s = malloc( sizeof( name ) );
    	x = malloc(sizeof(struct list1));
    	strcpy( s, name);
    	x->name=s;
    
    	x->next=NULL;
    	ptr=*node;
    
    	if(*node==NULL)
    		*node=x;
    	else{
    		while(ptr->next!=NULL)
    			ptr=ptr->next;
    		ptr->next=x;
    	}
    }
    
    int main(void)
    {
    	struct list1 *nList,*ptr;
    	FILE *fileptr;
    	char name[BUFSIZ],dump;
    	nList= NULL;
    
    	fileptr=fopen("namelist.txt","r");
    	if(fileptr==NULL)
    		perror("Unable to open file");
    	else{
    		while(fscanf(fileptr,"%[^\n]\n",name,&dump)!=EOF)
    		{
    			printf("%s\n",name);
    			addname(name,&nList);
    		}
    		for(ptr=nList;ptr;ptr=freemem1(&ptr))
    		{
    			printf("%s\n", ptr->name);
    		}
    	}
    	return 0;
    }

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >strcpy( s, name);
    You need to include <string.h>.

    >if(temp==NULL)
    This should test for inequality:
    Code:
    if (temp != NULL)
    That way you dereference temp and move foward, then release the memory if the pointer is not null rather than the other way around where you simply return a pointer to the node.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    Code:
    void addname(char name[BUFSIZ],struct list1 **node)
    {
    	struct list1 *x,*ptr;
    	char *s = malloc( sizeof( name ) );
    	x = malloc(sizeof(struct list1));
    	strcpy( s, name);
    	x->name=s;
    	x->next=NULL;
    	ptr=*node;
    
    	if(*node==NULL)
    		*node=x;
    	else{
    		while(ptr->next!=NULL)
    			ptr=ptr->next;
    		ptr->next=x;
    	}
    }
    Last edited by megablue; 07-14-2004 at 12:06 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You want to know one of your problems? *grin*
    Code:
    #include <stdio.h>
    
    void foo( char array[BUFSIZ] )
    {
            printf("%d\n", (int)sizeof array );
    }
    
    int main( void )
    {
            char buf[BUFSIZ];
            foo( buf );
            return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    ...erm.... got the idea... malloc(name[BUFSIZ]) doesn't allocate enough blocks of memory...
    but i still donno how to solve the problem... can you give me some guide

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try strdup

    and don't forget to free the memory...

  7. #7
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >Try strdup
    strdup isn't standard, so you can't be sure that the OP has it. Though it's trivial to write:
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    char *strdup(const char *str)
    {
      char *dup = malloc(strlen(str) + 1);
    
      if (!dup) {
        return NULL;
      }
      strcpy(dup, str);
    
      return str;
    }
    Of course, the name strdup is invading the implementation's namespace because any function name that begins with str and is followed by a lower case letter is reserved for future additions to <string.h>.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    thx...this is a nice function ^_^.. save me a lot of time

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    char *strdup(const char *str)
    {
      char *dup = malloc(strlen(str) + 1);
    
      if (!dup) {
        return NULL;
      }
      strcpy(dup, str);
    
      return dup;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >return dup;
    Hmm, I seem to be making more mistakes than usual. Maybe I'm losing my touch.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by megablue
    ...erm.... got the idea... malloc(name[BUFSIZ]) doesn't allocate enough blocks of memory...
    but i still donno how to solve the problem... can you give me some guide
    No, look at the following again:
    Code:
    void foo( char name[BUFSIZ] )
    {
        printf("%d is the sizeof a pointer, not the array\n", (int)sizeof name );
        printf("%d is the sizeof your array\n", (int)BUFSIZ );
    }
    The problem is, when you pass an array to a function, it is in effect converted to a pointer. As such, you cannot use sizeof on it and expect to get the size of the array. You can only use sizeof on arrays which are in the same scope as their declaration:
    Code:
    #include <stdio.h>
    
    char array[10];
    
    void foo( char array1[] )
    {
        printf("In function foo...\n\n");
        printf("The size of array is %d\n", (int)sizeof array );
        printf("This works because array is in scope for this function.\n\n");
        printf("The size of array1 is %d\n", (int)sizeof array1 );
        printf("This doesn't work because array1 is not in the same scope in which it was declared.\n");
    }
    
    int main( void )
    {
        char array1[12];
    
        printf("In main...\n\n");
        printf("The size of array is %d\n", (int)sizeof array );
        printf("This works because array is in scope for this function.\n\n");
        printf("The size of array1 is %d\n", (int)sizeof array1 );
        printf("This works because array1 is in scope for this function.\n\n");
    
        foo( array1 );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    oic... when an array passed into a function... it will be treated as a pointer in the function... and the sizeof array only return the size of the pointer itself not the allocated memory of the string.. isit?

  13. #13
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    I seem to be explaining this quite a bit these days, but except as the direct operand to sizeof, address-of (&) or a string literal initializer, array names are converted to a pointer to the first element. Those three situations are where the object is used rather than the value. So in the following code:
    Code:
    #include <stdio.h>
    
    void f(char passed[])
    {
      printf("%lu\n", (unsigned long)sizeof passed);
    }
    
    int main(void)
    {
      char direct[10];
    
      printf("%lu\n", (unsigned long)sizeof direct);
      f(direct);
    
      return 0;
    }
    sizeof direct results in the value of 10 being printed and sizeof passed results in the size of a pointer being printed (4 on my system). That's because passing an array as the argument to a function doesn't fit into any of the situations where an array is used in object context., so a pointer to the first element is passed.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    ....erm.. the pointer is the only passed in element right?

  15. #15
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >....erm.. the pointer is the only passed in element right?
    The pointer is the only argument passed, yes.

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. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM