variable argument lenth mpz init

enders game endersgame99@yahoo.com
Fri, 27 Dec 2002 12:45:10 -0800 (PST)


Hey, awhile ago I was thinking it would be nice to be
able to mpz_init() multiple mpz_ts in the same
function call, easier to type and more readable.  I
didn't know how to set up the variable argument list
stuff then, but I just learned and I wrote a working
function mpz_init_va(), that takes the number of mpzs
to initialize, and then pointers to them.

here it is with an example main, maybe this can be
useful to other people.

#include <stdarg.h>
#include <stdio.h>
#include <gmp.h>

mpz_init_va(int nargs, ...);

main()
{
	mpz_t a, b, c;

	mpz_init_va(3, &a, &b, &c);

	mpz_set_ui(a, 53);
	mpz_set_ui(b, 34);
	mpz_set_ui(c, 583);
	
	gmp_printf("a: %Zd b: %Zd c: %Zd\n", a, b, c);
}

mpz_init_va(int nargs, ...)
{
	va_list ap;

	va_start(ap, nargs);

	for(nargs; nargs > 0; nargs--)
		mpz_init(*va_arg(ap, mpz_t *));
	
	va_end(ap);
}

alternatively a function gmp_init() could be made with
a format string that calls the proper mpX_init()
function for the data type.  my applications only
require integers.  If one such function already
exists, I didnt see it in the manual.

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com