[英]How to use struct in different files c programming
The error I am getting is dereferencing pointer to incomplete type
but I have used the structure twice in another file and works perfectly fine. 我遇到的错误是dereferencing pointer to incomplete type
但是我在另一个文件中使用了两次该结构,并且工作得很好。 Why when I try to use it for the 3rd time in main I get this error? 为什么当我尝试第三次使用main时出现此错误? Obviously I am using a different name, meaning that is not exactly the same structure. 显然,我使用了不同的名称,这意味着结构不完全相同。
Here I define the structure 在这里我定义结构
//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
Here I use it twice, works perfectly 在这里我使用了两次,效果很好
//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;
}
Trying to use it 3rd time it gives me the error: dereferencing pointer to incomplete type
第三次尝试使用它给了我错误:将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;
}
If I try to use #include "bom.h" in main.c the error is multiple definition
如果我尝试在main.c中使用#include“ bom.h”,则错误为multiple definition
There are actually two problems in your code, and you need to fix both of them. 您的代码中实际上有两个问题,您需要同时解决这两个问题。 Fixing only one problem but not the other (which is essentially what you have tried) will not work. 仅解决一个问题而不解决另一个问题(本质上是您尝试过的问题)将无法工作。
1) At present createpoly()
is defined (aka implemented) in the header, so each compilation unit that #include
s that header will get its own definition - which causes the program not to link, in most circumstances. 1)目前, createpoly()
是在标头中定义的(也已实现),因此#include
s该标头的每个编译单元将获得其自己的定义-在大多数情况下,该程序不会链接。 The easiest fix for that is to only declare the function in the header, and define it in exactly one source file (which, preferably, will also include that header). 最简单的解决方法是仅在标头中声明该函数,并在一个源文件(最好也包含该标头)中定义它。 There are alternatives - for example, prefix the function definition with static
- but such options have other consequences (eg causing each object file to have its own local definition of the function) so are best avoided unless you have a specific need to do that. 还有其他选择-例如,为函数定义添加static
前缀-但此类选项会带来其他后果(例如,使每个目标文件具有其自己的函数本地定义),因此,除非有特殊需要,否则最好避免这样做。
2) A forward declaration is sufficient for declaring a pointer (eg struct polyinfo *s5
in your code) but not sufficient to dereference that pointer (eg printf("%d", s5->poly[i])
). 2)前向声明足以声明一个指针(例如,代码中的struct polyinfo *s5
),但不足以取消对该指针的引用(例如, printf("%d", s5->poly[i])
)。 The solution, in your case, is to include the header (with the definition of struct polyinfo
) within main.c
. 在您的情况下,解决方案是在main.c
包含标头(具有struct polyinfo
的定义)。
The multiple definitions linker errors come from defining a function in a header file. 多个定义链接器错误来自于在头文件中定义函数。
createpoly
to bom.c but keep a function prototype in bom.h. 将函数定义createpoly
移至bom.c,但将函数原型保留在bom.h中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.