Thread: Why my functions doesn't work?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    12

    Why my functions doesn't work?

    Code:
    void createList()
    {
    	struct node *ptr, *tmp;
    	int classList, i, head;
    
    
    	head = (struct node *)malloc(sizeof(struct node));
    
    
    	if(head == NULL)
    	{
    	    printf("Unable to locate list.");
    	    exit(0);
    	}
    
    
    	printf("Enter total number of list: \n",);
    	scanf("%d", &classList);
    
    
    	head -> classList;
    	head -> next = NULL;
    	tmp = head;
    
    
    	for(i=2; i<=n; i++)
    	{
    	    printf("Unable to allocate memory");
    	    exit(0);
    	}
    
    
    	printf("Enter total number of list %d:", i);
    	scanf("%d", &classList);
    
    
    	ptr -> studentData = classList;
    	ptr -> next = NULL;
    	tmp -> next = ptr;
    	tmp = tmp -> next;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why did you declare head to be a int?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile at a high warning level and pay attention to warnings.

    I notice that you aren't following my suggestion of separation of concerns by having an appendNode function. Furthermore, creareList needs to either return a node or change the head node by reference, otherwise you would need head to be a global variable, which is not a good idea.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my functions doesn't work as I want
    By Nuha in forum C Programming
    Replies: 2
    Last Post: 12-10-2013, 07:22 AM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. Trying to do basic math functions. Why doesn't this work?
    By crazychile in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:14 PM
  4. Why doesn't this work?
    By kabuatama in forum C Programming
    Replies: 5
    Last Post: 02-23-2006, 09:26 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread