0
votes

Let's say we have two structs in a source file:

struct B {
    int x;
};

struct A {
    beta y;
}; 

In the equivalent header file we have these:

typedef B* beta;
typedef A* alpha;

Also, these function prototypes are defined at the header file:

printAplha(alpha);
compare(const beta, const beta);

In the main file, I have included the specific header file and the code looks like this:

alpha one, two;
printAlpha(one);
printAlpha(two);
//everything works fine up to here
compare(one->y, two->y);

At the last line of the code I am getting

main.c:37:20: error: dereferencing pointer to incomplete type
main.c:37:33: error: dereferencing pointer to incomplete type

I know I can use a wrapper function for compare, the arguments of which would be of type alpha (as the arguments of the compare function cannot be changed - it's a recursive one), but I would like to see if there is any other solution, and why is this happening.

Note: The struct definitions have been written into the source file for creating an opaque data type.

2
Why is my question downvoted? Could Somebody provide a reason so as to know what I've done wrong?Chris

2 Answers

1
votes

There's certainly no "encryption" going on in what you're doing, but it is making it impossible for your example code to see the implementations of those structures. If you want to access fields in a structure, you need to make the complete structure definition visible to that translation unit. Otherwise, it's an incomplete type, just like the error message says.

1
votes

You're trying to declare an opaque data-type for struct A and struct B using the typedefs, but at the same time you're trying to access the fields using such an opaque type.

You can only do one of the above, not both simultaneously.

The solution is to either expose the complete struct definition or provide helper methods which will help access the specific fields. The implementation of the helper methods will again need to have access to the complete structure definition.