Search code examples
cgmp

Implementation of mdi_init of the gmp library in C


I am trying to implement my own version of the GMP mpz_t data type in C. For that , I am facing a problem in defining the structure for mpz_t.

This is because I am defining my mpz_t like this

typedef struct integer
{
int array[100];
int no_digits;
}mdi;

Clearly, I am storing my large values as an array of integer data types and no_digits is the number of digits in the structure. But now, for the init function , I have to meet the following prototype

void mdi_init(mdi x); // Initialises the data-type.

Here the return type is void and the input parameter is of type mdi. I am confused as to how to meet this requirement with my definition of mdi.

Help needed.


Solution

  • I am confused as to how to meet this requirement with my definition of mdi.

    You can't.

    structs are passed by value, so a function taking an mdi as an argument cannot change the passed argument.

    GMP defines mpz_t as an array (of length 1) of __mpz_structs,

    typedef __mpz_struct mpz_t[1];
    

    and __mpz_struct is analogous to your mdi struct. So mpz_init() receives a pointer to an __mpz_struct. You would need to do the same, you can make mdi_init() explicitly take a pointer to an mdi as argument, or, like GMP, make mdi an array (of length 1) of mdi_structs.