c - Is there a way to redefine a struct? -


i not trying have 2 different structs same name, rather defining same exact struct 2 different times (probably in different header files).

for example:

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

gives error.

the way surround struct 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 effect of defining struct foo once. in combination commonly accepted practice of putting such item in file called foo.h, so

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

it protects against person doing

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

as far redefining struct, not permitted in c language; however, can things approximate non-full redefine. recommend avoiding them, tend make code more obscure , difficult maintain.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -