Jump to content
  • Advertisement
Sign in to follow this  
Xiachunyi

C: Making a delcared struct global [Solved]

This topic is 4722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.h
struct 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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!