given that code, how would you make it possible for mystruct.c to use typedefs in main.c?

03-27-2003

Codeplug

If you have types that you want to make available to any module, then put them in their own .h file. If main.c and mystruct.c both want to use those types, then they will #include the .h file.

gg

03-27-2003

Hammer

Just nit-picking....

In this:

Code:

typedef struct _mystruct_t
{
byte a,b;
} mystruct_t;

The _t normally denotes a typedef. In your code, the struct is tagged as _mystruct_t, when of course it isn't actually a typedef name, so the use of _t is inappropriate. Also, you're running very close to the naming restrictions by using a leading underscore, I'd suggest you avoid them altogether.

My advice:

Code:

typedef struct mystruct
{
byte a,b;
} mystruct_t;

03-27-2003

Codeplug

Where I grew up, "_t" stands for "type", and "struct _mystruct_t" is a type.

I use leading underscores on my structure tags cuz MS does - any name collisions will be found at compile/link time anyway.

What-ever floats your boat :)

gg

03-27-2003

GhOsT_DeStRoYeR

ohh, ok

so its imposible to put them in seperate files. realy stops just about all code management....

03-28-2003

GhOsT_DeStRoYeR

Quote:

Read what codeplug said - you put shared declarations (typedefs, function prototypes, whatever) in a .h file, and #include it wherever you need it.

that is what i want to stop. if you do that then its not realy dividing the code up real well is it?? its just sticking it in one file. i read what codeplug said, but it doesnt suit what i'm after. i'm beginning to suspect what i want cant be done....

03-29-2003

GhOsT_DeStRoYeR

no, i have a header file for each c file. the header files contain the prototypes, extern's and typedefs.

the problem comes when i try to use one typedef in a procedure in a different c file. its basicly to do with the ordering of the header files. i need to know how to "prototype" a typedef. so that it doesnt stop with an error when it gets to one that is declaired later on.

03-29-2003

Hammer

>>so that it doesnt stop with an error when it gets to one that is declaired later on.
Are you using the include guards as shown in the above posts?

Code:

/* This is file myheader.h */
#ifndef MYHEADER
#define MYHEADER

/* Prototypes/typedefs etc go here */

#endif

04-01-2003

GhOsT_DeStRoYeR

ok, i apologise if i have been missleading in what i have said.

i'm trying (note the trying) to make a RPG adventure game similar to diablo, arcanum etc, so i want to keep all the code in smallish files so that its extremely easy to find things.

the problem comes when i deal with typedefs.

this is only an example, the real problem is spread over 4 files - Items.h, spells.h, Monsters.h and Character.h. Each header file contains an extern statement for the global variables declared in the file, and the prototypes for the functions. then there is Global.h which is included in every c file

basicly each file requires a typdef from another file, and nomatter what order they are placed in, atleast one typedef is required before it is declared

I keep the different typedefs in these files because thats what they relate to. This way is easier to change things (such as adding a new stat for the character, say Intelegence or something) because its under character.h and is only declared once.

if there is a way to make this work i would be most apreciative, but if not, any other suggestions would be handy. (and please dont say "wack it all in one header file, coz thats what i'm trying to stop)

Thx

04-01-2003

Codeplug

You are going to have a hard time maintaining header files that depend on each other like that. There is nothing wrong with having multiple .c files for a single .h file.

Having said that:

Macro's can't have a period in them - they follow the same rules as for variable names. "_Item.h_" -> "_Item_h_"

You declared "Itemtype", but referenced it as "ItemType"

Your individual header files should not include Global.h, Global.h is a convenience header to be used by application programs, not your library. A general rule of thumb is "only include header files that contain definitions that you need.

Which leads us to your double-dependency problem. Items.h needs types in Character.h, and Character.h needs types in Items.h. To make this work (even though I don't agree with the design) you have to pick one repugnancy to keep, and eliminate the other dependency using forward declarations.
In this example, we'll keep the "Character.h needs Items.h" dependency.

A few things to notice here:
- You have to have a structure tag for the CharacterPropertiesType typedef. This is because you can't have a forward declaration of a typedef.
- There is limited use of a forward declaration:
- You can only use it as a pointer (ie. the size of the forwarded declaration is not needed)
- You can not access any elements of the forwarded type

You should make all the individual header files private to your library and only expose Global.h since this method creates include dependencies. For example, an application can't just include Item.h - it will have to include Character.h as well in order to be able to use the Bonus pointer. These types of details are hidden in Global.h (I would rename Global to <your library name>)

gg

04-01-2003

GhOsT_DeStRoYeR

ok, thanks for you help, this is exactly what i'm looking for. i realise that there is no problem with using 1 header file for more than one c file, its just that i find the way i've currently got it set out much easier to navigate through, up untill now.

Thx about the _item_h_ period thing, never realised that you couldn't do that, but strangely everything's worked up till now.

one last question, what are forward declarations? i dont like using something that i dont understand.

and once again, Thanks for your help

04-01-2003

Hammer

>>#define _Item_h_
Now you've gone and done it :D You've used a reserved name for sure this time ;)

All names starting with an underscore followed by another underscore or an upper case letter are reserved for implementation use.

As this is a C forum, not an MS one, we should try and stick to the rules, imho. :) Else your boat might not float when it is supposed to!

04-01-2003

Codeplug

A forward declaration is a way of telling the compiler:

"Hey! compiler, you don't know anything about this type (size or members) - but you will, so compile this and shut up!"

As I said before, this basically limits you to only using the type as a pointer since a pointer is just a memory address.