结构体写在头文件中

AC_YE 2020-05-18 12:29:29
为什么我结构体写在自定义头文件中,然后在student.c用结构变量形参结果报错,但是如果我在student.c再加上结构体像这样,就可以运行
...全文
1473 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
dv_zheng 2020-05-23
  • 打赏
  • 举报
回复
感谢分享!!!
千梦一生 2020-05-19
  • 打赏
  • 举报
回复
Stu.h:
Struct student
{
int a;
int b;
}
函数声明
------------------------------
Stu.c:
#incldue"Stu.h"
函数定义
------------------------------
main.c :: include"Stu.h"
------------------------------
Stu.c里面那些好像不需要extern。我记得说:extern是声明时用的。说,这个东西啊编译器你现在在这个.c/.h中是找不到具体形态的,只告诉你有这么个东西,到时候其它模块那儿才是具体形态(定义)。
但你这里的Stu.c里面的变量、函数,似乎都是定义。所以看上去用不到extern啊
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 1 楼 自信男孩的回复:
你的.c文件有没有include头文件(.h)呢?
有写了#include“student.h”
自信男孩 2020-05-18
  • 打赏
  • 举报
回复
你的.c文件有没有include头文件(.h)呢?
自信男孩 2020-05-18
  • 打赏
  • 举报
回复
引用 9 楼 其实一开始我是拒绝的 的回复:
[quote=引用 8 楼 自信男孩的回复:][quote=引用 7 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 6 楼 自信男孩的回复:][quote=引用 5 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote]
#includestdio.h括号就不写了
#include “student.h”
int main
{struct student s【M】;
input(s);
output(s);
return 0;
}[/quote]
代码如果没有保密要求,建议都贴出来~多个文件的都贴出来[/quote]
没有什么保密的,就这些和图片里的[/quote]
你的student.c源文件里引入student.h头文件了吗?[/quote]
没有,还有在main.c里面也没引用这个“student.c”[/quote]
include是头文件,不需要Include .c文件
你的问题就是因为在student.c中没有引入student.h文件。

如果student.c中的函数在main函数里被调用,建议函数声明也放到student.h中。

qybao 2020-05-18
  • 打赏
  • 举报
回复
引用 13 楼 其实一开始我是拒绝的 的回复:
那什么时候要把“student.c”放到main.c中之前我看到过类似的

只要你控制好不发生重复函数定义,你把student.c方法main.c也可以,但是没必要,加入头文件就可以了(也就是只用函数声明就够了)
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 14 楼 qybao的回复:
[quote=引用 12 楼 qybao 的回复:]
[quote=引用 11 楼 其实一开始我是拒绝的 的回复:]
要把“student.c”也放到main里面?

不用,加入头文件就可以了
在student.c 开头加入 #include "student.h" //这个是为了解决你最开始说的为什么student.c不认识student结构体的问题
在main.c 开头也加入 #include "student.h" //这个是为了让你的main函数能正常调用input和output函数[/quote]
而且为了重复定义,你的student.h最好定一个宏
比如
student.h的内容为
#ifndef STUDENT_H //加个宏判断
#define STUDENT_H
#define M 5
struct student {
int num;
char name[10];
}
void input(struct student a[]);
void output(struct student a[]);
#endif[/quote] 我知道了,谢谢大佬
qybao 2020-05-18
  • 打赏
  • 举报
回复
引用 12 楼 qybao 的回复:
[quote=引用 11 楼 其实一开始我是拒绝的 的回复:]
要把“student.c”也放到main里面?

不用,加入头文件就可以了
在student.c 开头加入 #include "student.h" //这个是为了解决你最开始说的为什么student.c不认识student结构体的问题
在main.c 开头也加入 #include "student.h" //这个是为了让你的main函数能正常调用input和output函数[/quote]
而且为了重复定义,你的student.h最好定一个宏
比如
student.h的内容为
#ifndef STUDENT_H //加个宏判断
#define STUDENT_H
#define M 5
struct student {
int num;
char name[10];
}
void input(struct student a[]);
void output(struct student a[]);
#endif
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 12 楼 qybao的回复:
[quote=引用 11 楼 其实一开始我是拒绝的 的回复:]
要把“student.c”也放到main里面?

不用,加入头文件就可以了
在student.c 开头加入 #include "student.h" //这个是为了解决你最开始说的为什么student.c不认识student结构体的问题
在main.c 开头也加入 #include "student.h" //这个是为了让你的main函数能正常调用input和output函数[/quote] 那什么时候要把“student.c”放到main.c中之前我看到过类似的
qybao 2020-05-18
  • 打赏
  • 举报
回复
引用 11 楼 其实一开始我是拒绝的 的回复:
要把“student.c”也放到main里面?

不用,加入头文件就可以了
在student.c 开头加入 #include "student.h" //这个是为了解决你最开始说的为什么student.c不认识student结构体的问题
在main.c 开头也加入 #include "student.h" //这个是为了让你的main函数能正常调用input和output函数
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
要把“student.c”也放到main里面?
qybao 2020-05-18
  • 打赏
  • 举报
回复
把#include "student.h" 加到 student.c 和 main.c 里
否则你的main也会因为找不到student.c 的函数实现而出错(student.h只有函数声明,没有函数实现)
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 8 楼 自信男孩的回复:
[quote=引用 7 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 6 楼 自信男孩的回复:][quote=引用 5 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote]
#includestdio.h括号就不写了
#include “student.h”
int main
{struct student s【M】;
input(s);
output(s);
return 0;
}[/quote]
代码如果没有保密要求,建议都贴出来~多个文件的都贴出来[/quote]
没有什么保密的,就这些和图片里的[/quote]
你的student.c源文件里引入student.h头文件了吗?[/quote] 没有,还有在main.c里面也没引用这个“student.c”
自信男孩 2020-05-18
  • 打赏
  • 举报
回复
引用 7 楼 其实一开始我是拒绝的 的回复:
[quote=引用 6 楼 自信男孩的回复:][quote=引用 5 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote]
#includestdio.h括号就不写了
#include “student.h”
int main
{struct student s【M】;
input(s);
output(s);
return 0;
}[/quote]
代码如果没有保密要求,建议都贴出来~多个文件的都贴出来[/quote]
没有什么保密的,就这些和图片里的[/quote]
你的student.c源文件里引入student.h头文件了吗?
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 6 楼 自信男孩的回复:
[quote=引用 5 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote]
#includestdio.h括号就不写了
#include “student.h”
int main
{struct student s【M】;
input(s);
output(s);
return 0;
}[/quote]
代码如果没有保密要求,建议都贴出来~多个文件的都贴出来[/quote] 没有什么保密的,就这些和图片里的
自信男孩 2020-05-18
  • 打赏
  • 举报
回复
引用 5 楼 其实一开始我是拒绝的 的回复:
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote]
#includestdio.h括号就不写了
#include “student.h”
int main
{struct student s【M】;
input(s);
output(s);
return 0;
}[/quote]
代码如果没有保密要求,建议都贴出来~多个文件的都贴出来
AC_YE 2020-05-18
  • 打赏
  • 举报
回复
引用 3 楼 自信男孩的回复:
[quote=引用 2 楼 其实一开始我是拒绝的 的回复:]
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分[/quote] #includestdio.h括号就不写了 #include “student.h” int main {struct student s【M】; input(s); output(s); return 0; }
qybao 2020-05-18
  • 打赏
  • 举报
回复
引用 2 楼 其实一开始我是拒绝的 的回复:
有写了#include“student.h”

你是在哪个文件里写了#include "student.h"的?
从你的截图来看,student.c里没有#include "student.h"
自信男孩 2020-05-18
  • 打赏
  • 举报
回复
引用 2 楼 其实一开始我是拒绝的 的回复:
[quote=引用 1 楼 自信男孩的回复:]你的.c文件有没有include头文件(.h)呢?

有写了#include“student.h”[/quote]
把代码都贴出来吧,尤其是头文件部分

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧