0

I have wrote some files: main.c, functions.c, functions2.c, and header.h. The some functions in the functions.c, and functions2 use my some enums, and structures.

Where must I place my enums, and structures? How to write declarations for them in the functions.c, and functions2.c? My functions (from different files) have to see them.

For example, I have wrote such function's declarations in the header.h:

int func(void);
void func2(int);

But I don't know how it write for enums, and structures.

Regards

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • 3
    have you thought about a *header file* (maybe the same one where your prototypes are located), then including that in your `.c` files ? ? – WhozCraig Feb 08 '13 at 20:32
  • @WhozCraig If I place it in the header file, then definitions of my enums, and structures will copied in the each .c-file. Will I have problems in this case in the future? – Andrey Bushman Feb 08 '13 at 20:38
  • 1
    @Bush use header guard : http://en.wikipedia.org/wiki/Include_guard – ablm Feb 08 '13 at 20:41
  • @Bush abim has commented on, and answers below have included, using header guards to ensure multiple inclusion of header data is only pulled into a source file once. Between that and the proper syntax of declaring enums and structs, you should be good to go. – WhozCraig Feb 08 '13 at 20:45
  • @Bush And even a header guard isn't necessary. What we are talking about here are declarations. –  Feb 08 '13 at 20:45

2 Answers2

1

Example for functions.c:

#include "header.h"

int func(void)
{
 ...
}

void func2(int)
{

}

Example for header.h:

#ifndef HEADER_H
#define HEADER_H

int func(void);
void func2(int);

enum eMyEnum
{
 eZero = 0,
 eOne,  
 eTwo
};

struct sMyStruct
{ 
 int i;
 float f;
};

#endif
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
1

Declaring structures:

typedef struct <optional struct name>
{
   int    member1;
   char*  member2;

} <struct type name>;

Put whatever members you want in the struct in the format above, with any name you want. Then you use:

<struct type name> my_struct;

To declare instances of the struct.

Declaring enums:

typedef enum
{
    value_name,
    another_value_name,
    yet_another_value_name

} <enum type name>;

Put whatever values in the enum as above, with any name you want. Then you use:

<enum type name> my_enum;

To declare instances of the enum.

PQuinn
  • 992
  • 6
  • 11