Thread: Variable Argument List

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Variable Argument List

    Is there an alternative method to determine no. of arguments passed to function that accepts a variable argument list ?

    i read va_end is a macro that set the pointer declared by va_list to NULL after no more item is left to be traveresed. I tried this, but i get a runtime crash

    Code:
    // Variable No. of Arguments Passed To Function
    
    #include<stdio.h>
    #include<stdarg.h>
    
    int findmax(int n,...);
    
    main(void)
    {
         int max;     
         max = findmax(3,50,66,12);
            printf("Max:%d",max);
         max = findmax(5,10,12,5,23,100);
            printf("\n\nMax:%d",max);
         getchar();
    }
    
    int findmax(int num,...)
    {
        int max,n,k=1;
        
        va_list ptr;                     // declaring pointer of type va_list
        va_start(ptr,num);         // initializing pointer to beginning of variable argument list
        
        max = va_arg(ptr,int);           // retrieve value, and increment the pointer
        
        while(ptr!=NULL)
        {
             n = va_arg(ptr,int);
             if(n>max)
                        max = n; 
             k++;       
        }
        return(max);     
    }
    i have just change while(k<=num) in my code to find for an alternative solution

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    ptr never changes. You should not be checking it. Instead, you should be checking the return value of va_arg().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Is there an alternative method to determine no. of arguments passed to function
    > that accepts a variable argument list ?
    There are three ways
    1. The way you've done it, pass a known parameter which counts the number of variable arguments.
    2. Like printf/scanf, which encode the number of variable arguments in the number of &#37; characters present in the control string.
    3. Like execl(), which specifically mark the end of the list of char pointers with a NULL pointer.

    > i read va_end is a macro that set the pointer declared by va_list to NULL
    If va_start is a "(" then va_end is a ")"
    It exists to provide balance to va_start, nothing more. It doesn't in itself provide any magical way of working out where the end of the argument list is.

    So you code should be
    - va_start
    - some kind of loop processing va_arg
    - va_end
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 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