c - 从函数返回结构

标签 c function structure

下面是我从函数返回结构的代码。可能问题很基础,但我不清楚为什么要在 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/

相关文章:

C++ if 循环和函数

c - 运行时错误 - c 中的指针

Mysql - 创建一个 stub 表来保存另一个表中某些行的ID

c++ - 您如何在托管代码环境之外安全地进行编程?

clang 链接错误 : undefined reference to 'qsort'

excel - 内置 Excel 函数在 VBA 代码中不起作用 - 我的错误是什么?

c - 将字符串分配给 C 结构中的元素

c - 如何显示和替换员工数据?

c - NtWriteFile - ERROR_INVALID_PARAMETER(错误 87)

c - #define 使用结构化指针中的成员