请大家说说 结构体 是放在头文件中好? 还是放在源文件好呢?
在昨天, 我在网上看了一下关于C语言的模块化设计的一些文章,
现在, 就想问各位 你们的结构体的申明放在什么地方好, 好在什么地方,
我先来说说, 在头文件中放置 结构体的定义有什么缺点.
1. 在头文件放结构体的话, 会把整个结构体信息放在头文件中, 会把整个结构的信息暴露出来.这样不利于模块化.
呵呵, 不会吧,怎么我就说出了一点缺点..
如果放在源文件中, 这个缺点给补上了.
对这个结构体的使用, 可以完全的通过结构体所在模块提供一定的函数来操作....
但是我这样做遇到了一点问题, 不知道该怎么处理
下面是一个有问题的例子.
//filename :temp_main.c#include "./temp.h"int main(void ) { struct temp a = creat_temp("wangxinglong", 12); display(a); return 1;}//filename: temp.c#include <stdio.h>#include "./temp.h"struct temp { char name[255]; int id;};int creat_temp(char *name, int id){ struct temp z; sprintf(z.name, "%s", name); z.id = id; return 1;}int display(struct temp a){ printf("My name is: %s\n My id is: %d\n", a.name, a.id); return 1;}//filename temp.h#ifndef TEMP_H#define TEMP_Hstruct temp;int creat_temp(char *name, int id);int display(struct temp a);#endif