我正在建立一个定义多项式的结构,即它包含两个变量:
-int degree
包含多项式的阶数
-int * coeff = (int*) malloc (degree * sizeof(int))
包含所有系数
我还定义了一个函数new_polynome()
,该函数接受一个度数,并返回一个指向该结构的指针,该结构保存该度数的多项式并将其所有系数都设置为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;
}
但是,当我尝试打印其系数时,出现以下错误。有正确的方法吗?我不明白该错误信息。 如何打印其系数?
我的错误如下:
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);
^~
答案 0 :(得分:2)
后面的错误通常是更早的结果,这是常见的情况。您对多义词的定义有误。结果是它没有名为coeff的成员。
您不能以您的方式初始化结构。删除结构中的malloc调用,您将看到其他一些错误将神奇地消失。
这行:new_polynome = new_polynome(5);
没有意义。似乎您正在尝试为5个coeff分配空间,但是那完全是错误的。您应该喜欢这个`new_polynome-> coeff = malloc(5 * sizeof(*(new_polynome-> coeff))
您应该移动一些代码,例如分配coeffs。那应该放在new_polynome函数中,我将其重命名为create_polynome。
工作(和固定)代码:
#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
}
我还做了一些很好的(主观地说)修改,例如在解引用器*和标识符之间没有空格。还消除了功能和感觉之间的空间。另外,我使用对象的大小而不是类型。
作为一个额外的奖励,该函数派生自:
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;
}
答案 1 :(得分:1)
1)int * coeff = (int *) malloc (degree * sizeof(int));
在结构定义中的语法无效:
typedef struct
{
int degree;
int * coeff = (int *) malloc (degree * sizeof(int));
} polynome;
应该是:
typedef struct
{
int degree;
int * coeff;
} polynome;
2)这是错误的:
polynome * new_polynome = (polynome *) malloc (sizeof(polynome));
new_polynome = new_polynome(5);
您正在将malloc
的结果分配给new_polynome
,然后立即用返回值new_polynome(5).
覆盖它,这是内存泄漏。
3)我想您可能想为N+1
整数而不是N
分配空间。零级多项式是一个常数。
答案 2 :(得分:1)
这是无效的语法;你可以写
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;
}
而不是。也许,当n
是不受信任的用户输入时,检查是否溢出。