Is hidden declaration possible in a project?

203 Views Asked by At

In my project, a structre is being used in several functions.

like this:

void function1 (Struct_type1 * pstType1);

but when I search for Struct_type1 's references, I can't find any. This Structure must be defined somewhere. How to find the definition?

OS- Windows

Edit: I think its difficult to answer this without source code and I can't share that big project here. So, I've changed my question to:

  • Is Hidden Declaration possible in an embedded project?

(by hidden I mean no one can see the definition.)

2

There are 2 best solutions below

10
On

Is Hidden Declaration possible in an embedded project?

If you have access to all source code in the project, then no.

This is only possible in one specific case, and that is when you have an external library for which you don't have the C code, you only have a header file and an object file or lib file (or DLL etc).

For such cases it is possible (and good practice) for the library header to forward-declare an incomplete type in the header, and hide the actual implementation in the C file which you don't have access to.

You would then have something like this in the h file:

typedef struct Struct_type1 Struct_type1;

The compiler might often do things like this with its own libraries too, if they want to hide away the implementation. One such example is the FILE struct.

5
On

Not an answer, but possibly a way to find the answer. Idea: Let compiler help you.

Define the struct yourself, then look at compiler errors like "struct struct_type1 is already defined in... at line ..."

If you get no compiler error in this case, maybe the struct is only forward declared, but not defined.

To explain why this is sometimes done, here a bit of code:

// Something.h

struct struct_type1; // Forward declaration.
struct struct_type1 *SomethingInit();
void SomethingDo( struct struct_type1 * context );

In code looking like the above, the definition of the struct is hidden inside the implementation. On the outside, it need not be known, how the struct is defined or its size etc, as it is only traded as a pointer to the struct (and never as a value). This technique is used to keep internal types out of public header files and used often by library designers. You can think of it as an opaque handle of sorts.

But then, you still should be able to find the forward declaration, albeit not the definition.