Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a defined struct from another source file?

Tags:

c

linux

I am using Linux as my programming platform and C language as my programming language.

My problem is, I define a structure in my main source file( main.c):

struct test_st
{
   int state;
   int status;
};

So I want this structure to use in my other source file(e.g. othersrc.). Is it possible to use this structure in another source file without putting this structure in a header?

like image 491
domlao Avatar asked Jun 15 '10 00:06

domlao


People also ask

Can struct be copied?

We can also use assignment operator to make copy of struct. A lot of people don't even realize that they can copy a struct this way because one can't do same it with an array. Similarly one can return struct from a function and assign it but not array. The compiler do all the copying stuff behind the scene for us.

Can you put a struct in a .h file?

If a struct is declared in a header file in C++, you must include the header file everywhere a struct is used and where struct member functions are defined. The C++ compiler will give an error message if you try to call a regular function, or call or define a member function, without declaring it first.

Should I define structs in header or source?

If the struct is to be used by other compilation units (. c files) , place it in the header file so you can include that header file wherever it is needed. If the struct is only used in one compilation unit (. c file), you place it in that .

Can we use extern for structure in C?

You cannot extern a struct directly. My favorite way to typedef the struct and then extern the instantiation. Actually it works the way you propose. But i organize the code as headers files and source files.


2 Answers

You can define the struct in each source file, then declare the instance variable once as a global, and once as an extern:

// File1.c
struct test_st
{
   int state;
   int status;
};

struct test_st g_test;

// File2.c
struct test_st
{
   int state;
   int status;
};

extern struct test_st g_test;

The linker will then do the magic, both source file will point to the same variable.

However, duplicating a definition in multiple source files is a bad coding practice, because in case of changes you have to manually change each definition.

The easy solution is to put the definition in an header file, and then include it in all the source file that use the structure. To access the same instance of the struct across the source files, you can still use the extern method.

// Definition.h
struct test_st
{
   int state;
   int status;
};

// File1.c
#include "Definition.h"
struct test_st g_test;

// File2.c
#include "Definition.h"  
extern struct test_st g_test;
like image 196
lornova Avatar answered Sep 21 '22 11:09

lornova


You can use pointers to it in othersrc.c without including it:

othersrc.c:

struct foo
{
  struct test_st *p;
};

but otherwise you need to somehow include the structure definition. A good way is to define it in main.h, and include that in both .c files.

main.h:

struct test_st
{
   int state;
   int status;
};

main.c:

#include "main.h"

othersrc.c:

#include "main.h"

Of course, you can probably find a better name than main.h

like image 32
Matthew Flaschen Avatar answered Sep 22 '22 11:09

Matthew Flaschen