【发布时间】:2018-06-28 10:31:01
【问题描述】:
我得到的错误是dereferencing pointer to incomplete type,但我在另一个文件中使用了两次该结构并且工作得很好。为什么当我第三次尝试在 main 中使用它时出现此错误?显然我使用了不同的名称,这意味着它的结构不完全相同。
我在这里定义结构
//bom.h
#ifndef BOM_H_INCLUDED
#define BOM_H_INCLUDED
struct polyinfo {
int size;
int poly[];
};
struct polyinfo *createpoly(struct polyinfo *s, int sz, int p2[]){
int i;
s=(int*)malloc(sizeof(*s) + sizeof(int)*sz);
s->size=sz;
for(i=0;++i<sz;)
s->poly[i]=2*p2[i];
return s;
};
int* bom(int s[], int n);
#endif // BOM_H_INCLUDED
这里我用了两次,效果很好
//bom.c
#include <stdio.h>
#include "bom.h"
int* bom(int s[], int n){
int i;
int *s2;
struct polyinfo *s3;//using the structure of polyinfo
struct polyinfo *s4;//using the structure of polyinfo 2nd time
s4 = createpoly(s4, n, s);//creating a poly multiply by 2
printf("printing 2nd:");
for(i=0;++i<n;)
printf("%d", s4->poly[i]);
printf("\n");
s2=(int*)malloc(n*sizeof(int));
printf("received n= %d\n",n);
for(i=0;++i<n;)
printf("%d", s[i]);
printf("\n");
for(i=0;++i<n;)
s2[i]=2*s[i];
s3 = createpoly(s3, n, s);//creating a poly multiply by 2
printf("printing the struct, poly size: %d\n",s3->size);
for(i=0;++i<n;)
printf("%d ", s3->poly[i]);
printf("\n");
return s2;
}
第三次尝试使用它给了我错误:dereferencing pointer to incomplete type
//main.c
#include <stdio.h>
int main(){
int i, s[]={1,1,1,0,1};//the pattern that will go
int n=sizeof(s)/sizeof(*s);//size of the pattern
int *p;//sending the patt, patt-size & receiving the poly
struct polyinfo *s5;//using the structure of polyinfo 3rd time
s5 = createpoly(s5, n, s);//creating a poly multiply by 2
printf("printing 2nd:");
for(i=0;++i<n;)
printf("%d", s5->poly[i]);
printf("\n");
p=bom(s, n);
for(i=0;++i<n;)
printf("%d", p[i]);
return 0;
}
如果我尝试在 main.c 中使用 #include "bom.h" 错误是 multiple definition
【问题讨论】:
-
你有多少
main.c? -
为什么
createpoly在标题中? -
@Sourav Ghosh- 只是一个,@StoryTeller - 因为 poly 是一个灵活的数组成员,我用它来分配内存和初始化结构
-
是的,我明白了。仍然没有解释为什么它在标题中定义。
-
对于什么头文件,你得到多个定义错误?
标签: c data-structures malloc codeblocks