0

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); ^~

Avocado
  • 93
  • 8
  • 5
    Members cannot be initialized within the struct's definition. `coeff` should be initialized in `new_polynome`. This code is allocating memory for two `polynome` and discarding the first one. The `new_polynome` variable is masking the function of the same name. – cfillion Apr 02 '19 at 21:30

3 Answers3

2

It is common that latter errors is just a consequence of earlier, and this is the case here. You have a faulty definition of polynome. This has the consequence that it does not have a member called coeff.

You cannot initialize a struct the way you do. Remove the malloc call in the struct, and you'll see that a few other errors will magically disappear.

And this line: new_polynome = new_polynome(5); does not make sense. It seems like you're trying to allocate space for 5 coeffs, but then you're completely wrong. You should do like this `new_polynome->coeff = malloc(5*sizeof(*(new_polynome->coeff))

Also, don't cast malloc

There is some code you should move, like allocation of coeffs. That should go inside the new_polynome function, which I renamed to create_polynome.

Working (and fixed) code:

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

typedef struct
{
    int degree;
    int *coeff; // No init here. You're just defining a type here.
} polynome;


polynome *create_polynome(int n)
{
    // A polynomial of degree 0 is a constant
    const int N = n+1;

   // You need to set the degree
    new_polynome->degree = n;

    polynome *new_polynome = malloc(sizeof(*new_polynome));
    new_polynome->coeff = malloc(N * sizeof(*(new_polynome->coeff)));
    for(int i = 0; i < N; i++)
        new_polynome->coeff[i] = 1;

    return new_polynome;
}

int main(void)
{
    polynome *new_polynome = create_polynome(5);
    // Looping to degree makes much more sense
    for(int i = 0; i < new_polynome->degree+1; i++)
        printf("%d", new_polynome->coeff[i]);

    free(new_polynome->coeff);
    // Not necessary to free in the end, but if you want to do it,
    // do it properly and free everything. One malloc for each free and
    // vice versa
    free(new_polynome);
    // No need  to return 0 since that's the default in main
}

I also made some good (subjectively speaking) modifications like not having a space between the dereferencer * and the identifier. Also removed space between functions and parethesis. Also, I used the size of the object instead of the type.

And as an extra bonus, a function that derivates:

polynome *derivate_polynom(polynome *pol)
{
    polynome * derivate = create_polynome(pol->degree - 1);
    for(int i = 0; i < derivate->degree + 1; i++)
        derivate->coeff[i] = (i+1) * pol->coeff[i+1];
    return derivate;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
1

1) int * coeff = (int *) malloc (degree * sizeof(int)); is invalid syntax inside your struct definition:

typedef struct 
{
    int degree;
    int * coeff = (int *) malloc (degree * sizeof(int));
} polynome;

Should just be:

typedef struct 
{
    int degree;
    int * coeff;
} polynome;

2) This is wrong:

polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
new_polynome = new_polynome(5);

You are assigning the result of malloc to new_polynome and then immediately overwriting it with the return value of new_polynome(5). This is a memory leak.

3) I think you might want to allocate space for N+1 integers instead of N. A zero-degree polynomial is a constant.

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
1

It is invalid syntax; you can write

typedef struct 
{
    int degree;
    int coeff[];
} polynome;

polynome *new_polynome(int n)
{
    polynome *p;

    p = malloc(sizeof *p + n * sizeof p->coeff[0]);
    for(int i = 0; i < n; i++)
        p->coeff[i] = 1;

    return p;
}

instead of. Perhaps, check for overflows when n is untrusted user input.

ensc
  • 6,704
  • 14
  • 22