9

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?

afuzzyllama
  • 6,275
  • 5
  • 42
  • 61
domlao
  • 14,295
  • 31
  • 88
  • 124

7 Answers7

19

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;
lornova
  • 5,917
  • 8
  • 39
  • 68
  • 1
    @kuhaku: you should define the structure in a header file. Duplicating the definition is a very bad programming practice, as you have to manually change each definition in case of changes. – lornova May 29 '15 at 22:39
17

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

Matthew Flaschen
  • 255,933
  • 45
  • 489
  • 528
  • like in othersrc.c, I have to define struct test_st *lcst; ? – domlao Jun 15 '10 at 00:18
  • the forward declaration is unneeded, just declare pointers. so you could write a function `void foo(test_st *lcst);` however you can see any of the feilds inside the structure, so anything that needs to work with the data inside it needs to be in main.c or you can put the structure definition in a header – Spudd86 Jun 15 '10 at 13:39
5
// use a header file.  It's the right thing to do.  Why not learn correctly?

//in a "defines.h" file:
//----------------------

typedef struct
{
   int state; 
   int status; 
} TEST_ST; 


//in your main.cpp file:
//----------------------

#include "defines.h"

TEST_ST test_st;


    test_st.state = 1;
    test_st.status = 2;




//in your other.ccp file:

#include "defines.h"

extern TEST_ST test_st;

   printf ("Struct == %d, %d\n", test_st.state, test_st.status);
Grimper
  • 61
  • 1
4

Putting it in a header file is the normal, correct way to declare types shared between source files.

Barring that, you can treat main.c as a header file and include it in the other file, then only compile the other file. Or you can declare the same struct in both files and leave a note to yourself to change it in both places.

drawnonward
  • 52,256
  • 15
  • 103
  • 110
2

C support separate compilation.

Put structure declaration into a header file and #include "..." it in the source files.

Nikolai Fetissov
  • 77,392
  • 11
  • 105
  • 164
0

It is perfectly reasonable to be inclusive with structs by leaving them in the source file instead. This is encapsulation. However if you're going to redefine struct multiple times in multiple source files then you might as well define the struct once in a header file instead and include that file as necessary.

OS2
  • 373
  • 2
  • 10
-2

Header file /* include this header file in both file1.c and file2.c

strcut a {

};

struct b {

}; 

so header file included the declaration of both structures .

file 1.c 

strcut a xyz[10]; --> struct a defined here

to use struct b here in this file

extern struct b abc[20];

/* now can use in this file */

file2.c

strcut b abc[20]; /* defined here */

to use strcut a defined in file1.c

use extern struct a xyz[10]
Mihriban Minaz
  • 3,005
  • 2
  • 30
  • 51