I saw the post at question mpz_t to unsigned long long conversion (gmp lib) and Chris Jester-Young gave me the answer
mpz_t ull2mpz(unsigned long long ull)
{
char buf[40];
int len;
mpz_t result;
len = snprintf(buf, sizeof buf, "%llx");
if (len >= sizeof buf) { /* oops */ }
mpz_init(result);
len = gmp_sscanf(buf, "%Zx", result);
if (len != 1) { /* oops */ }
return result;
}
The problem here is that, as stated in How to convert GMP C parameter convention into something more natural? mpz_t is an array. How can I circumvent this(Without doing so strange things, just returning a value)? If I write instead
void mpz_set_ull(mpz_t val, unsigned long long ull){
char buf[40];
int len;
mpz_t result;
len = snprintf(buf, sizeof buf, "%llx");
if (len >= sizeof buf) { /* oops */ }
mpz_init(result);
len = gmp_sscanf(buf, "%Zx", result);
if (len != 1) { /* oops */ }
mpz_set(val,result);
}
I get wrong results.
And, is his code legal C?
OP is not using
snprintf()
correctly. Need to passull
.Use