Variadic function for multiplications with mpz_t

Marc Glisse marc.glisse at
Fri Jan 9 09:06:01 UTC 2015

On Thu, 8 Jan 2015, Ha T. Lam wrote:

> 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_srcptr actually, because it is const.

> mpz_init(num);

Why? You are not creating a new number, just referring to an existing one. 
The absence of a corresponding mpz_clear is a big hint that something is 

Marc Glisse

More information about the gmp-discuss mailing list