Thread: variable-length argument list ???

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Question variable-length argument list ???

    hi, i heard it's possible to do functions with variable-length argument list. is that true? if so, where can i find more info about it?

    i mean, how are they prototyped etc

    thanx

    null

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Very true....

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    // sum a list of integers
    // the list ends when a zero is detected
    int sum_args ( int first, ... ) {
        va_list ap;
        int     val, sum = first;
        va_start(ap,first);
        while ( (val=va_arg(ap,int)) != 0 ) {
            sum += val;
        }
        va_end(ap);
        return sum;
    }
    
    int main() {
        printf( "%d\n", sum_args(1,2,3,4,5,6,0) );
        return 0;
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Thumbs up

    hi Salem, thanks for the example. just a couple of questions on it...the:

    while ( (val=va_arg(ap,int)) != 0 )

    is it automaticaly incremented (to point to the next argument) ?

    also all the:

    va_...

    are they global? which geader file are they in?

    also, on a different subject, do you know of any good advanced c book? i can only find books with the usual stuff (vars, pointers, structs,functions,loops etc). i'm looking for something that picks where the usual books stop and goes from there. something with real-life problems and solutions, like good ways of programming certain things.

    It seems that good books on c are occupying less space on bookstore shelves now...it's just full of vb, databases, java, web stuff etc...

    thanks for the help

    null

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I'm not trying to answer for Salem or anything. I admit, he is one of the best programmers here. Anyway, since this is a public board, and I've used va_arg a couple of times although not so elegantly as Salems code, yes, it does search for the number of arguements by incrementing through them using pointers.

    I heard that it is utilized practicaly by library functions such as printf that have a control string. Otherwise use a linked list.

    Regarding books, try "C Unleashed". It is certainly and advanced C book. And no I have not read it all. I have looked through it. I know for a fact it's a great book.

    Anyway there is a point that I would like to make. If you want to become a great C programmer than investigate C++. Doing this will improve your C skills because C++ in a small to medium way is the art of building smart functions using C.
    Last edited by Troll_King; 10-14-2001 at 07:18 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > va_start(ap,first);
    Uses the last named argument (first) to establish a pointer to the first unnamed argument, and stores that reference in ap.

    > (val=va_arg(ap,int))
    Returns the current argument (in this case of type int), and advances ap to the next argument.

    > va_end(ap);
    Generally tidies up any loose ends.

    If you were using a printf type control string (%s, %d etc), then you would perhaps have some kind of if/case statement to call
    (s_ptr=va_arg(ap,char*)) // %s
    (d_val=va_arg(ap,int)) // %d
    As appropriate

    > are they global? which geader file are they in?
    <fx: takes a wild stab in the dark>
    #include <stdarg.h>
    Wasn't it obvious this would be the answer?

    As for books - hard to say
    http://www.accu.org/bookreviews/publ...advanced_c.htm
    might be helpful in perhaps choosing books to actually go and look at before making a decision.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Unhappy

    lol...

    yes, that was very obvious...sorry man...and thanks for replying


    null

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM