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
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
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.
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
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.
> 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.
lol...
yes, that was very obvious...sorry man...and thanks for replying
null