return mpz_t from a function

4k Views Asked by At

I am experiencing a problem while using mpir library in C++. Is it possible to return mpz_t value from a function? When I try to do it, I have the following error:

RSA.cpp:50:36: error: ‘HASHtemp’ declared as function returning an array
mpz_t RSA::HASHtemp( mpz_t message )
1

There are 1 best solutions below

2
On BEST ANSWER

No, it's not possible. The type mpz_t is defined as an array type:

typedef __mpz_struct mpz_t[1];

and a function cannot return an array.

It means that you can define an object of type mpz_t and then pass it as an argument to a function, allowing the function to modify its value (since arrays decay to pointers).

In a comment, you wrote:

I do not understand why there are function declarations who are said to return mpz_t in the documentation.

The documentation shows a couple of macros, not actual functions, mpq_numref and mpq_numden, that it describes as returning mpz_t values. In fact they both yield a result whose type is a pointer to the element type of the mpz_t array (__mpz_struct*). That value can be passed to a function that's documented as taking an mpz_t argument, but in fact all such functions take pointer arguments.

C and C++ do not permit either parameters of array type or functions returning array values, but they have several features that let you write code that looks as if that were possible. An expression of array type is, in most contexts, converted to a pointer, and a function parameter of array type is "adjusted" to be a pointer parameter. (Personally, I'm not a huge fan of the way GMP / MPIR takes advantage of this.)