0

I'm getting the error passing argument 1 of ‘add_to_polynomial’ from incompatible pointer type but, I'm pretty sure I'm not. But I'm pretty new to C, this is for an assignment.

The Header, polynomial.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

This is inside of a different file - polynomial.c

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

I've tried all sorts of stuff, over the past few days but I finally gave up and came here. It's saying that poly->next is an incompatible pointer type... but it's a pointer to another instance of itself, so how is this not working? Which is all the error means right? It thinks that poly->next isn't a poly_t? also how come struct poly_t* is different from poly_t* aren't they the same thing?

First time ever asking a question on here, sorry if I'm not providing enough info or something in advance.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
yoconn
  • 13
  • 3

1 Answers1

1

In this typedef definition

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

you declared an unnamed structure the type of which has the alias poly_t and a named structure struct poly_t in the declaration of the data member

  struct poly_t* next;

Instead write

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

Take into account that this function

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

does not make sense. All it does is finds a null pointer in the list.

It seems you mean something like the following

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);
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • oh jeeze, I could have sworn I tried that but I guess not. Thank you so much! Also thanks for the speedy reply! I'll accept this in ~3mins when it lets me :) – yoconn Feb 23 '20 at 19:26