1

I am not trying to have two different structs with the same name, but rather defining the same exact struct two different times (probably in different header files).

For example:

struct foo { int bar; }; struct foo { int bar; }; 

gives an error.

2

4 Answers 4

2

The way to do this is to surround your struct with preprocessor instructions

#ifndef STRUCT_FOO #define STRUCT_FOO struct foo { int bar; }; #endif /* STRUCT_FOO */ #ifndef STRUCT_FOO #define STRUCT_FOO struct foo { int bar; }; #endif /* STRUCT_FOO */ 

this has the effect of only defining struct foo once. In combination with the commonly accepted practice of putting such an item in a file called foo.h, like so

#ifndef INCLUDE_FOO_H #define INCLUDE_FOO_H struct foo { int bar; }; #endif /* INCLUDE_FOO_H */ 

it also protects against a person doing

#include "foo.h" #include "foo.h" (rest of code) 

As far as redefining the struct, that is not permitted in the C language; however, you can do some things that approximate a non-full redefine. I recommend avoiding them, as they tend to only make the code more obscure and difficult to maintain.

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

1 Comment

I do not see the point. When you are just "worried" to not include the same struct in multiple places, then just one guard is sufficient. What is the point to have 2 guards for the same macro? The second one is redundant and can be omitted here. There is no way the second guard will process, so your example lack the intend of OP. Unless the OP asked about "how to protect against redefining" then "how to actually redefine struct".
2

No.

There is absolutely no point in doing this. If you have a structure that is to be used by multiple compilation units, put it in a .h header file, and #include it from those .c files. Or, if you need it in multiple header files, just include the common header file from those.

Now if you don't need the actual structure definition, but rather just need to declare that it exists (so you can create pointers to said struture), you can use a forward declaration:

 struct foo; // defined elsewhere void somefunc(struct foo *ptr); 

1 Comment

@whoKnows Well you never indicated what your use case was, so there's no way we could know.
2

Short answer: No

The compiler isn't that smart - you already have struct foo, so you can't have another struct foo even if you think it is the same as the first one.

Comments

1

No.

You should seperate the struct in another header, it sounds like you may be organizing your code poorly and should rethink the design.

You could use a ifndef:

#ifndef NAMEDEF struct name { int val; }; #define NAMEDEF #endif 

Although, I must reiterate that you need to rethink how your header files are designed and put this struct in a common header.

Its also possible to use a foreward declaration:

struct name; void function() { } 

3 Comments

That would 1) result in problems if name was already defined before as a macro or even another object. 2) it would defy any usage of the struct, as every occurence of name afterwards would be replaced by <nothing>. But your point about the headers might be worth to elaborate.
I intended the define to use all caps to prevent a name collision with the struct. I just corrected this.
It is not just caps, but, yes, that would work. It is called guard, btw. and the same as used for a header file. Still more of a hack, than a clean solution; that should simply go into a single header (which would then have its guard).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.