【问题标题】:passing argument 1 of ‘ ’ from incompatible pointer type - C [duplicate]从不兼容的指针类型传递“”的参数 1 - C [重复]
【发布时间】:2020-06-07 11:47:22
【问题描述】:

我收到错误从不兼容的指针类型传递“add_to_polynomial”的参数 1,但我很确定我不是。但我对 C 很陌生,这是一个作业。

标题,多项式.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

typedef struct {
  void * data;
  struct poly_t* next;
} poly_t;
void add_to_polynomial(poly_t * poly, const term_t * term);    
#endif

这是在另一个文件中 - polynomial.c

void add_to_polynomial(poly_t* poly, const term_t* term) 
{
  if(poly->next != NULL)
  { add_to_polynomial(poly->next, term); }
}

在过去的几天里,我尝试了各种各样的东西,但我最终放弃了,来到了这里。就是说 poly->next 是一个不兼容的指针类型......但它是一个指向自身另一个实例的指针,那么这怎么不起作用?哪个是所有错误的意思是正确的?它认为 poly->next 不是 poly_t?还有struct poly_t*poly_t*怎么不一样?

第一次在这里提问,如果我没有提前提供足够的信息或其他内容,请见谅。

【问题讨论】:

    标签: c pointers struct declaration typedef


    【解决方案1】:

    在这个 typedef 定义中

    typedef struct {
      void * data;
      struct poly_t* next;
    } poly_t;
    

    您在数据成员的声明中声明了一个未命名结构,其类型具有别名 poly_t 和命名结构 struct poly_t

      struct poly_t* next;
    

    改为写

    typedef struct poly_t {
      void * data;
      struct poly_t* next;
    } poly_t;
    

    考虑到这个功能

    void add_to_polynomial(poly_t* poly, const term_t* term) 
    {
      if(poly->next != NULL)
      { add_to_polynomial(poly->next, term); }
    }
    

    没有意义。它所做的只是在列表中找到一个空指针。

    你的意思好像是这样的

    void add_to_polynomial( poly_t **poly, const term_t *term ) 
    {
        if ( *poly == NULL )
        {
            *poly = malloc( sizeof( poly_t ) );
            ( *poly )->data = term;
            ( *poly )->next = NULL;
        }
        else
        {
            add_to_polynomial( &( *poly )->next, term);
        }
    }
    

    【讨论】:

    • 哦,天哪,我可以发誓我试过了,但我想没有。太感谢了!也感谢您的快速回复!我会在大约 3 分钟内接受它:)
    猜你喜欢
    • 2018-02-27
    • 2013-12-25
    • 2017-12-11
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多