Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top