Leo
Leo

Reputation: 97

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. Why when I try to use it for the 3rd time in main I get this error? 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

//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

Upvotes: 0

Views: 317

Answers (2)

Lundin
Lundin

Reputation: 213318

The multiple definitions linker errors come from defining a function in a header file.

  • Include the header file from all files using it.
  • Move the function definition createpoly to bom.c but keep a function prototype in bom.h.

Upvotes: 0

Peter
Peter

Reputation: 36597

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 #includes that header will get its own definition - which causes the program not to link, in most circumstances. 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 (e.g. 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.

2) A forward declaration is sufficient for declaring a pointer (e.g. struct polyinfo *s5 in your code) but not sufficient to dereference that pointer (e.g. printf("%d", s5->poly[i])). The solution, in your case, is to include the header (with the definition of struct polyinfo) within main.c.

Upvotes: 1

Related Questions