3

I'm working on a project in which I'm forced to use some previously written code that uses many header files calling other header files. I'm trying to keep my application separated, but I still need to use many of the types and functions defined in the previous code.

I've added forward declarations in my own header files, when referencing data types declared in other header files. My problem now, is I'm trying to reference a union type from within a function definition in a source file and I'm not quite sure how to do it.

Old header file:

/* filename: include.h */

typedef union SOME_UNION_TYPE
{
  int a;
  .
  .
}SOME_UNION_TYPE;


My source file:

/* filename: file.c */

void func()
{
  SOME_UNION_TYPE A;
  .
  .
  return;
}


I know it would be easier to just include "include.h" in my source code, but I'm trying to avoid it as much as possible. My attempt so far has been to forward declare the union in my header file, in hopes of exposing the typedef name to the compiler.

My header file:

/* filename: file.h */

union SOME_UNION_TYPE;


However, when I compile, I receive an error complaining about unknown size for A in func().

Thanks in advance for any help.

2
  • 5
    You can't allocate something that forward declared. You can however create a pointer to whatever is being forward declared. Commented Dec 2, 2013 at 23:32
  • @NoahWatkins thanks! I'd assumed that forward declaration in the header provided size info during compile time. I need to spend some time getting a better understanding of the compiling and linking process. Commented Dec 4, 2013 at 16:30

3 Answers 3

3

If you need to create an actual instance of the type, you need the complete declaration, not just the name.

The right thing to do in this case is to bite the bullet and include the header file that defines the type.

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

3 Comments

To add on this: You can't expect the compiler to allocate a storage for a variable without knowing how long it has to be! That's why you can forward declare structs and define pointers to them, since all pointers are the same length; but you can't define a struct of said type.
@caf thanks. It looks like I have the option of using a pointer to the type, but does this actually gain me anything, aside from speeding up compilation?
@dsell002: No, and there's only very limited things you can do with a pointer to an incomplete type.
3

The #include directive is not something to be avoided. If you're concerned about other people's code polluting your code, you can make a header that includes the headers you need, and include that one directly.

Duplicating code is bad.

1 Comment

Thanks for the response! Can you explain what you mean by duplicating code? I hadn't intended on rewriting the code. Thanks.
1

The other code probably includes a bunch of other headers because it needs them! You should just include the other header otherwise you are duplicating code which is bad.

3 Comments

Your correct. It definitely needs the other files, but in many cases there are headers files that reference each other. I'm not the best programmer, but to me it just seems like poor design.
It could be a poorly written library that pulls in too much, but you can't fix that. Avoiding the #include and duplicating code instead is even worse!
+1 to @John3136. Code duplication is the plague. See if you can take a time off coding to fix the existing headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.