function questions

This is a discussion on function questions within the C Programming forums, part of the General Programming Boards category; Why would we create a function: list_head *create_list(void) Purpose: allocate memory for a new list header Return: a pointer to ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    function questions

    Why would we create a function:

    list_head *create_list(void)
    Purpose: allocate memory for a new list header
    Return: a pointer to a list header

    I don't understand the use of it.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    I take it this is with reference to linked lists inserting problem
    The create_list() function in that code does nothing useful, it just creates a junk node at the head of the list.

    A true empty list is
    list_head *head=NULL;

    And your insert_to_list() function already knows how to insert into an empty list with the if(*head==NULL) test.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks I get it now.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    I have one more question. I am making a calulator for long intergers the operations I can perform are + and *;

    I am having problem making an add function. I don't know what to do I am lost.
    The functions that I am stuck in are:

    int add_two_digits(int d1, int d2, int *carry_in, int * result, int *carry_out);
    list_head * add_two_numbers (list_head N1, list_head N2);

    I tried to do the formating of the code but since I bring from textpad to this bord the formating becomes horrible.

    The code that I have now is:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define FALSE 0
    char c = '\0';
    int currentChar = 0;
    int numOfCharsInC = 0;
    
    typedef struct node
    {
      int integerone;
      struct node * link;
    } list_head;
    list_head*list1,*list2,*result;
    
    //Concating the 2 lists together
    list_head * concat(list_head *p,list_head *q)
    {
    	list_head *x,*r;
    	if (p==NULL)
    	   r=q;
    
    	if (q==NULL)
    	     r=p;
    	else
    	{
    	    x=p;
          	     r=x;
          	while(x->link!=NULL)
    	    x=x->link;
         	    x->link=q;
    	}
        return(r);
    }
    
    //Inserting in the list
    int insert_to_list(list_head**head,int digit){
      list_head *newNode= malloc(sizeof(list_head));
    
      newNode->integerone=digit;
      newNode->link=NULL;
    
      if(*head==NULL){
        *head=newNode;
    return 1;
      }
      else{
        list_head*temp=*head;
        while(temp->link!=NULL){
          temp=temp->link;
    
        }
        temp->link=newNode;
    
      }
      return 0;
    }
    
    //Menu choices
    void options(){
        printf("\n1. Insert first number\n");
        printf("1. Insert second number\n");
        printf("3. Add numbers\n");
        printf("4. Multiply numbers\n");
        printf("5. Process file\n");
        printf("6. Quit\n");
        printf("Enter Option: ");
    	scanf("%c",&c);
        fflush(stdin);
    }
    
    //Printing the list Contents
    void printList(list_head*head){
      list_head*temp=head;
      while(temp){
        printf("%d " ,  temp->integerone);
        temp=temp->link;
      }
    
    }
    //Reverses the list
    list_head *reverse_list(list_head *head){
    	list_head  *p,*q,*r;
    	p=head;
    	q=NULL;
    
    	while(p!=NULL)
    	{
    	    r=q;
    	    q=p;
    	    p=p->link;
    	    q->link=r;
    	}
    	head = q;
    	printList(head);
    }
    
    //Add two digits <!!!!!THIS IS THE FUNCTION THAT I NEED HELP WITH!!!!!!!>
    
    int add_two_digits(int d1, int d2, int *carry_in, int * result, int *carry_out){
    *carry_in=0;
    
    for (i=0;; i++) {
    	*result =*result = d1+d2+*carry_in;
                      if(result>=10){
    	 *result=*result-10;
    	 *carry_out=1;
    	}
    	else{
    	   *carry_out=0;
    	}
    
    }
    
    
    
    //allocate memory for a new list header
    list_head *create_list(void){
      list_head*p=NULL;
      p=malloc(sizeof(list_head));
    
      p->link=NULL;
    
      return p;
    }
    
    //Main function
    int main(void){
         options();
          while (c!='6'){
             while ((currentChar = getchar()) != '\n' && currentChar != EOF){
     	if(numOfCharsInC==0) {
                   	    c = currentChar;
    	}
    	numOfCharsInC++;
            }
    
         if(c=='1'){
             fflush(stdin);
             printf("\n1. Insert first number\n");
             scanf("%c",&c);
             fflush(stdin);
             do{
                 insert_to_list(&list1,c-'0');
    
             } while((c = getchar()) != '\n');
             numOfCharsInC = 0;
             options();
        }
    
         else if(c=='2'){
              fflush(stdin);
              printf("\n2. Insert second number\n");
              scanf("%c",&c);
              fflush(stdin);
              do{
    	  insert_to_list(&list2,c-'0');
                  } while((c = getchar()) != '\n');
    	                   
              numOfCharsInC = 0;
             options();
    
           }
    
    
         else if (c== '3'){
    		fflush(stdin);
            printf("\n3. Add numbers\n");
    		numOfCharsInC = 0;
    		options();
    
    	 }
    
         else if(c== '4'){
    		fflush(stdin);
            printf("\n4. Multiply numbers\n");
            numOfCharsInC = 0;
    		options();
    
    	 }
    
         else if(c=='5'){
    		fflush(stdin);
            printf("\n5. Process file\n");
            numOfCharsInC = 0;
    		options();
    
         }
         else if(c=='6'){
    		fflush(stdin);
    		numOfCharsInC = 0;
    		options();
            break;
         }
         else{
    
            printf ("Invalid menu choice - try again\n");
            break;
         }
      }
      return 0;
    }

    Thanks alot for help. I really appreciate it.
    Sara
    Last edited by sara.stanley; 04-02-2006 at 03:39 AM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Um...uh...hmm...ahh...

    </offtopic>





    So what exactly is wrong? The algorithm? The code? Segfault?
    Last edited by jafet; 04-03-2006 at 03:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21