【问题标题】:How to use struct in different files c programming如何在不同的文件中使用struct c编程
【发布时间】: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


【解决方案1】:

您的代码中实际上存在两个问题,您需要同时修复它们。只解决一个问题而不解决另一个问题(这基本上是您尝试过的)是行不通的。

1) 目前createpoly() 是在标头中定义(又名实现)的,因此#includes 该标头的每个编译单元都将获得自己的定义 - 在大多数情况下,这会导致程序无法链接。最简单的解决方法是仅在头文件中声明函数,并将其定义在一个源文件中(最好也包含该头文件)。还有其他选择 - 例如,在函数定义前加上 static - 但这些选项会产生其他后果(例如,导致每个目标文件都有自己的函数本地定义)所以最好避免,除非你有特殊需要做那个。

2) 前向声明足以声明指针(例如代码中的 struct polyinfo *s5),但不足以取消引用该指针(例如 printf("%d", s5-&gt;poly[i]))。在您的情况下,解决方案是在main.c 中包含标题(定义为struct polyinfo)。

【讨论】:

    【解决方案2】:

    多定义链接器错误来自在头文件中定义函数。

    • 包括所有使用它的文件的头文件。
    • 将函数定义createpoly 移至bom.c,但在bom.h 中保留函数原型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多