C: Making a delcared struct global [Solved]

Started by
1 comment, last by Xiachunyi 18 years, 10 months ago
Hello, is it possible to declare a structure globally in a header file so that all files that include it have access to its members? Example: Header.h

struct
{
    int MemberOne;
    float MemberTwo;
    char MemberThree;
} Stuff;
SourceOne.c and SourceTwo.c both have

#include "Header.h"
...
//Member manipulation
I tried just adding the structure to my header file, I ony have one at the moment, and my compiler throws up a redundant declaration error. Thank you. [Edited by - Xiachunyi on June 20, 2005 5:23:46 PM]
Advertisement
You need to declare that it exists by name in your header file, but define it only once, in only one .c file.
//Definition.hstruct s_Stuff{    int MemberOne;    float MemberTwo;    char MemberThree;};extern struct s_Stuff Stuff;//Declaration.c#include "Definition.h"struct s_Stuff Stuff;//Usage.c#include "Definition.h"int main(){  Stuff.MemberOne = 1;  Stuff.MemberTwo = 2.0f;  Stuff.MemberThree = '3';  return 0;}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Thank you very much for replying so quickly; it works wonderfully.

This topic is closed to new replies.

Advertisement