Variadic function for multiplications with mpz_t

Ha T. Lam hatlam at gmail.com
Thu Jan 8 21:44:24 UTC 2015


Hi,

I'm trying to write a varadic function for multiplication mod n because
mpz_mul only multiplies two variables at a time. My function looks like
this:

void mpz_muls_mod(mpz_t result, const mpz_t mod, const mpz_t x,...)
{
    mpz_t num;
    mpz_init(num);
    mpz_set(result,x);
    va_list args;
    va_start(args,x);
    while ((num=va_arg(args,mpz_t))!=NULL)
    {
        mpz_mul(result,result,num);
        mpz_mod(result,result,mod);
    }
    va_end(args);
}

I get error: "invalid array assignment" at  num=va_arg(args,mpz_t). I'm
guessing that this is because va_arg returns a pointer and mpz_t is an
array. I tried to change things to use pointers instead, but I don't know
how to init an mpz_t*. I finally stumbled into mpz_ptr when I read the
implementation of inits, but I get a seg fault when doing things like:

mpz_ptr num;
mpz_init(num);

Any idea what to try next?

Thanks,

Ha


More information about the gmp-discuss mailing list