[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Doubts: I wish you could help me
From: |
Kevin Ryde |
Subject: |
Re: Doubts: I wish you could help me |
Date: |
22 Nov 2001 07:43:16 +1000 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) Emacs/20.5 |
"MFRC" <address@hidden> writes:
>
> 1. First of all, I would be very thankfull if you could explain to me how I
> can build a function that makes any arithmetical operation and returns an
> mpz_t type value.
You probably don't want to do that, but instead should store to an
output parameter, like the gmp library functions do.
> mpz_t mpzAdd(mpz_t a, mpz_t b)
> {
> mpz_t p;
> mpz_init (p);
> mpz_add (p, a, b);
> return p;
> }
"p" is local to that function and is no longer valid when it returns.
If you really want to do something like that then the space for the
mpz_t will have to come from malloc, or be static, or similar. The
return type probably has to be "mpz_ptr" or "MP_INT *" too. But as I
say, normally an extra parameter is probably better,
void mpzAdd(mpz_t p, mpz_t a, mpz_t b)
{
mpz_init (p);
mpz_add (p, a, b);
}
In general it'll be more flexible to let the caller do the mpz_init
call, but it works to have that in the function, if you want.
> 2. Aditionally, if I define an mpz_t type in a typedef, or in a union, or in a
> struct, when will I initialize the variable?
Any time before you use it, same as a plain mpz_t.