Answer the question
In order to leave comments, you need to log in
One-time structure declaration in the project
There are, let's say, three files:
blah.c
blah.h
main.c
in blah.h
the structure is declared:
structure blah test_blah (...);
in blah.c
it is described (and the file is included blah.h
).
In main.c is included blah.h
In this case, when trying to compile these files, the compiler complains about the duplicate declaration of the structure test_blah
.
Therefore, I write instead structure blah test_blah (...);
static structure blah test_blah (...);
of But in this case, 2 independent instances are created test_blah
(in blah.c
and main.c
), which is not desirable.
How to make it so that only 1 instance is declared test_blah
inmain.c
Answer the question
In order to leave comments, you need to log in
You are creating a variable, not just declaring a structure. test_blah
is a variable, data type struct blah
. It was probably thought like this:
/* blah.h */
struct blah; /* урезанное описание */
/* blah.c */
#include "blah.h"
struct blah { int a, b } /* описание */
test_blah; /* и тут же объявление переменной этого типа */
/* main.c */
#include "blah.h"
/* можно работать с указателями на struct blah */
Not "description" and "description", but declaration ( declaration ) and definition ( definition ).
If you have a type ( struct blah
) and you need one instance ( instance ) (in your case, test_blah ) of this type, which you need to have access to from anywhere in the program, then you need to define this instance once (of course, you won’t be able to , ODR Define - write the full name of the type, specifying the name of the variable in one translation unit), and before using declare, previously indicating that the variable (instance) is "external" ( extern) in relation to the current translation unit (usually, this is a separate .cpp file after the preprocessor).
As a result, you have, roughly speaking, 2 translation units ( blah.c , main.c ). You need to define a variable in any of them (at the point, of course, when the type of the variable is defined, not declared). And, for convenience (so that later you can just include the header file, and not write the same thing every time), declare a variable in blah.h , and by including blah.h in any desired .cpp file, use it. Those.:
- // blah.h
- #ifndef BLAH_H
- #defineBLAH_H
- // We declare and define the type (you can only declare it, but define it elsewhere)
- struct blah
- {
- int value ;
- } ;
- // Declare a variable, indicating that it is defined elsewhere
- // (Because at this point we have a complete type definition,
- // you can declare blah_test of type struct blah, and if the definition
- // was in a different place (i.e. would just have struct blah; above), then you would have to
- // declare a pointer to struct blah, i.e. struct blah*)
- extern struct blah blah_test ;
- #endif
- // ---------------------------------
- // blah.cpp (blah.c)
- #include "blah.h"
- // Define a variable
- struct blah blah_test ;
- // ---------------------------------
- // main.cpp (main.c)
- // Including the header file, we declare a variable,
- // which we want to use.
- #include "blah.h"
- int main ( )
- {
- // Use
- blah_test. value = 0 ;
- return 0 ;
- }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question