问:当你从“结构__mpz_struct *”中分配“mpz_t”类型时,不兼容的类型?

时间:2022-07-15 16:13:34

I am trying to learn how to use GNU GMP library in c, I wrote this program to see how mpz_t mpq_numref( mpq_t N) and mpz_t mpq_denref( mpq_t N) work. I get an error, and really don't know how should I modify the code so that it works.

我正在尝试学习如何在c中使用GNU GMP库,我编写了这个程序来查看mpz_t mpq_numref(mpq_t N)和mpz_t mpq_denref(mpq_t N)的工作。我得到了一个错误,我真的不知道该如何修改代码以使其工作。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>

int main()
{ 

  mpq_t u, v;
  mpz_t a,b,c;
  mpz_init(a);
  mpz_init(b);

  mpq_init( u );
  mpq_init( v );

  mpq_set_si( u, -6 ,2);
  mpq_canonicalize( u );
  a= mpq_numref( u );
  b= mpq_denref( u );
  gmp_printf( "u =%Zd/%Zd\n", a,b );

  return 0;
}

The error I get is:

我得到的错误是:

error: incompatible types when assigning to type ‘mpz_t’ from type ‘struct __mpz_struct *’
       a= mpq_numref( u );
        ^
7.c:21:8: error: incompatible types when assigning to type ‘mpz_t’ from type ‘struct __mpz_struct *’
       b= mpq_denref( u );
        ^

Thank you very much for any help

非常感谢您的帮助

2 个解决方案

#1


0  

Careful reading of the error message will give you the answer here. The mpq_numref and mpq_denref functions both return a pointer to the __mpz_struct type -> struct __mpz_struct *.

仔细阅读错误消息将在这里给出答案。mpq_numref和mpq_denref函数都返回指向__mpz_struct类型的指针——> struct __mpz_struct *。

Pay attention to the type signature of your functions and datatypes, there is no implicit conversion possible between a type T and it's corresponding pointer type T *.

注意函数和数据类型的类型签名,类型T和对应的指针类型T *之间不可能进行隐式转换。

#2


0  

Thanks the comment of M.M the code should be so modified, and it works.

谢谢M的评论。代码应该如此修改,并且它是有效的。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <gmp.h>

    int main()
    { 

      mpq_t u, v;
      long int x,y,z;
      mpz_t a,b,c;
      mpz_init(a);
      mpz_init(b);

      mpq_init( u );
      mpq_init( v );

      mpq_set_si( u, -6 ,2);
      mpq_canonicalize( u );
      mpz_set( a, mpq_numref( u ) );
      mpz_set( b, mpq_denref( u ) );
      gmp_printf( "u =%Zd/%Zd\n", a,b );

      return 0;
    }

#1


0  

Careful reading of the error message will give you the answer here. The mpq_numref and mpq_denref functions both return a pointer to the __mpz_struct type -> struct __mpz_struct *.

仔细阅读错误消息将在这里给出答案。mpq_numref和mpq_denref函数都返回指向__mpz_struct类型的指针——> struct __mpz_struct *。

Pay attention to the type signature of your functions and datatypes, there is no implicit conversion possible between a type T and it's corresponding pointer type T *.

注意函数和数据类型的类型签名,类型T和对应的指针类型T *之间不可能进行隐式转换。

#2


0  

Thanks the comment of M.M the code should be so modified, and it works.

谢谢M的评论。代码应该如此修改,并且它是有效的。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <gmp.h>

    int main()
    { 

      mpq_t u, v;
      long int x,y,z;
      mpz_t a,b,c;
      mpz_init(a);
      mpz_init(b);

      mpq_init( u );
      mpq_init( v );

      mpq_set_si( u, -6 ,2);
      mpq_canonicalize( u );
      mpz_set( a, mpq_numref( u ) );
      mpz_set( b, mpq_denref( u ) );
      gmp_printf( "u =%Zd/%Zd\n", a,b );

      return 0;
    }