请大家说说 结构体 是放在头文件中好? 还是放在源文件好呢?

mew mew 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在昨天, 我在网上看了一下关于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_H

struct temp;

int creat_temp(char *name, int id);
int display(struct temp a);

#endif


这个问题是在编译, 根本就通过不了, 请各位支支招.
...全文

相关推荐

评论 43
收藏 已收藏
分享
返回顶部
mew
C语言 70,039 成员 243,237 内容