I'm setting up a struct that defines a polynome, that is it contains two variables:
-int degree
that contains the degree of the polynome
-int * coeff = (int*) malloc (degree * sizeof(int))
that holds all the coefficients
Also I have defined a function new_polynome()
that takes in a degree and returns a pointer to a struct that holds a polynome of that degree with all its coefficients set to 1;
#include <stdio.h>
#include <stdlib.h>
// 1
typedef struct
{
int degree;
int * coeff = (int *) malloc (degree * sizeof(int));
} polynome;
// 2
polynome * new_polynome(int n)
{
polynome * newest_polynome = (polynome *) malloc (sizeof(polynome));
for(int i = 0; i < n; i++)
newest_polynome->coeff[i] = 1;
return newest_polynome;
}
int main(void)
{
polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
new_polynome = new_polynome(5);
for(int i = 0; i < 5; i++)
printf("%d", new_polynome->coeff[i]);
free(new_polynome->coeff);
return 0;
}
However when I try and print its coefficients I get the following error. Is there a right way to do this? I don't understand the error message. How do I print its coefficients?
My error is as follows:
TD_polynomes.c:9:17: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
int * coeff = (int *) malloc (degree * sizeof(int));
^
TD_polynomes.c: In function ‘new_polynome’:
TD_polynomes.c:19:24: error: ‘polynome {aka struct <anonymous>}’ has no member named ‘coeff’
newest_polynome->coeff[i] = 1;
^~
TD_polynomes.c: In function ‘main’:
TD_polynomes.c:27:20: error: called object ‘new_polynome’ is not a function or function pointer
new_polynome = new_polynome(5);
^~~~~~~~~~~~
TD_polynomes.c:26:16: note: declared here
polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
^~~~~~~~~~~~
TD_polynomes.c:30:34: error: ‘polynome {aka struct <anonymous>}’ has no member named ‘coeff’
printf("%d", new_polynome->coeff[i]);
^~
TD_polynomes.c:32:22: error: ‘polynome {aka struct <anonymous>}’ has no member named ‘coeff’
free(new_polynome->coeff);
^~