0

I have a typedef for a struct in sampleHeader.h that is similar to:

typedef struct example Example;

and I have in my sampleSource.c:

struct example{
    char a[4];
    char b[4];
    char c[5];
}

Now for some reason when I return a pointer back to my main function which references a struct that has been created ( and malloc'd) and try to print out the values of each member I get an error along the lines of "cannot derefence incomplete type"

Any ideas?

4
  • 2
    Can we see the bit of code that actually gets the "cannot dereference incomplete type" error? Commented Oct 8, 2013 at 20:21
  • Take a look here: bbs.archlinux.org/viewtopic.php?id=141686 Commented Oct 8, 2013 at 20:27
  • We need to see the code, but you need a ';' at the end of that struct definition. Commented Oct 8, 2013 at 20:29
  • No need for code, this is a simple scope issue. main.c can't see the definition, because there is no definition in the header. Commented Oct 9, 2013 at 2:54

2 Answers 2

1

In the header file you have only forward declared the struct. That's fine and you'll be able to declare pointers and references to the struct in the header (and any other header or cpp file that includes this header too).

As the compiler has only seen the definition in the cpp module, this is the only place that you'll be able to declare variables of type struct example by value or to dereference pointers to access members. Outside of the cpp file the compiler doesn't know how big the struct is or what is members are.

If you need to use the struct in multiple modules declare and define the struct together in the header.

Sign up to request clarification or add additional context in comments.

Comments

0

Hard to say for sure without seeing the actual code, but....

struct example{
    char a[4];
    char b[4];
    char c[5];
};
 ^ note the new semi colon.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.