Advertisement Jump to content
Sign in to follow this  
Xiachunyi

C: Making a delcared struct global [Solved]

This topic is 4959 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!