下面是我从函数返回结构的代码。可能问题很基础,但我不清楚为什么要在 struct emp putinfo (){
这一行中写 emp
。我不清楚为什么我需要写结构名称?因为在函数内部,我将结构定义为 struct emp t;
。具体原因是什么?
#include <stdio.h>
struct emp {
int number;
char name [200];
};
void print (struct emp team){
printf ("Details of employee %d %s", team.number, team.name );
}
struct emp putinfo (){
struct emp t;
printf ("Enter number"); scanf ("%d", &t.number);
printf ("Enter name"); scanf ("%s", &t.name);
return t;
}
int main (){
struct emp test;
test = putinfo ();
print (test);
return 0;
}
最佳答案
struct emp
实际上是你在 C 中寻址结构的名称。你可以 typedef
它然后你可以只写 emp
.之所以放在putinfo
之前是因为这是函数返回的类型。struct emp t
只是新建了一个emp
类型的结构体> 并将其分配给 t
。
关于c - 从函数返回结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802040/