Thread: stack cant find null

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    stack cant find null

    Can somebody help me to find out why does my loop go forever, even when im telling it to stop once it finds NULL?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    struct user
    {
    	int  number;
    	char name[15];
    	struct user* next;
    	
    };
    
    struct user* push( struct user* , char * , int fd);
    
    int main()
    { 
    
    	char add2[20]   = "MATT";
    	char add4[20]   = "STEVE";
    	
    	struct user * client;
    	struct user * node;
    	
    	client = NULL;
    	
    	client = push(client, add2, 5);
    	client = push(client, add4, 5);
    
    	for ( node = client; node != NULL; node = client->next)
    	{
    		printf("%s\n", node->name);
    		printf("%d\n", node->number);
    	}
    	
    	return 0;
    }
    
    struct user* push( struct user* current_user, char * username , int number) {
      struct user* newtop;
      newtop = (struct user*)malloc(sizeof(*newtop)); 
      
      strncpy(newtop->name, username, strlen(username) + 1);
      newtop->number = number;
      newtop->next = current_user;
      
      return newtop;
    
    }
    Last edited by charlitos; 04-03-2009 at 04:06 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing headers, without them, NULL isn't actually defined, and it doesn't compile. push is defined after you use it. There is nothing called fd in your structure, so you can't print it. You use size 15 and size 20 for name, in different places. This is just asking for your program to crash. And finally, it should be:
    Code:
    node = node->next
    I'd advise you to start using warnings when you compile, and to listen to them and fix them as you encounter them.


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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    i included all the headers
    and the function prototype is also on top

    the fd is not like that on my code i just made a mistake after i copied

    anyway i edited my code the same way I have it, so all these problems you mentioned are not problems anymore, still it loops forever

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because when you were editing your code in your first post again, you didn't fix what I specifically pointed out:
    Code:
    for ( node = client; node != NULL; node = client->next)

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

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    Thumbs up

    oh sorry, i didnt notice that one, thanks a lot
    Last edited by charlitos; 04-03-2009 at 04:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM