我希望这个问题有一个简单的答案,所以请耐心等待我。如果我有一个C头文件定义类型为这样:C:创建一个结构体,返回一个TYPE
struct Example {
char description[EXAMPLE_DESC_SIZE];
int val;
};
typedef struct Example Example;
# ifndef TYPE
# define TYPE Example
# define TYPE_SIZE sizeof(Example)
# endif
然后,在.c文件,我有一个函数,如下所示:
TYPE createExample (int val, char *desc) {
}
其从主称为
TYPE newEx;
char desc[EXAMPLE_DESC_SIZE], filename[50], *nlptr;
int val;
newEx = createExample(val, desc);
如何编写createExample以便它返回一个TYPE?我试过以下(和其他几个不成功的尝试):
TYPE createExample (int val, char *desc)
{
TYPE temp;
struct Example example;
example->val = val;
strcpy(example->description, desc);
return temp = example;
}
我在发帖前试过这个,得到了上面提到的错误。任何想法为什么? – user2103459 2013-02-24 00:21:38
@ user2103459您需要更改'#define',请参阅编辑。 – Dukeling 2013-02-24 00:25:49
@ user2103459注意到了哪里?看不到任何错误消息。 – junix 2013-02-24 00:26:45