0

我编写了一些文件:main.c、functions.c、functions2.c 和 header.h。functions.c 和 functions2 中的一些函数使用了我的一些枚举和结构。

我必须在哪里放置我的枚举和结构?如何在functions.c 和functions2.c 中为它们编写声明?我的功能(来自不同的文件)必须看到它们。

例如,我在 header.h 中编写了这样的函数声明:

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

但我不知道它是如何为枚举和结构编写的。

问候

4

2 回答 2

1

functions.c 的示例:

#include "header.h"

int func(void)
{
 ...
}

void func2(int)
{

}

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
于 2013-02-08T20:38:30.547 回答
1

声明结构:

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

} <struct type name>;

将您想要的任何成员以上述格式放入结构中,并使用您想要的任何名称。然后你使用:

<struct type name> my_struct;

声明结构的实例。

声明枚举:

typedef enum
{
    value_name,
    another_value_name,
    yet_another_value_name

} <enum type name>;

将任何值放在上面的枚举中,使用您想要的任何名称。然后你使用:

<enum type name> my_enum;

声明枚举的实例。

于 2013-02-08T20:39:42.900 回答