c - C语言中如何编写 "takes 2 integers, returns a Point_t representing these integers"函数?

标签 c function structure

typedef struct { int x; int y; } Point_t;
Point_t construct_point(int x, int y); /* takes 2 integers, 
                                          returns a Point_t representing 
                                          these integers */

这个函数怎么写?

最佳答案

这真的很简单:

typedef struct
{
    int x;
    int y;
} Point_t;

Point_t f(int x, int y)
{
    Point_t p = { x, y };

    return p;
}

并且(为了预先避免可能的注释):不,这不会返回对局部变量的引用。

关于c - C语言中如何编写 "takes 2 integers, returns a Point_t representing these integers"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728698/

相关文章:

c++ - 在此c++代码中乘以结构成员是什么意思

c - GCC 编译器允许结构转换,但 Visual Studio 不允许

c - 尝试打印目录和所有子目录中以字节为单位的总空间

c++ - 如何在 c 中转换 std::numeric_limits<float>::epsilon()?

c - 我想将 gdb 用于一个 c 文件,该文件具有类似 #include "config.h"的头文件?

c - 如何将结构体数组传递给函数

python - 指定函数中的输入类型

javascript - 参数作为函数名

c++ - 在 C++ 中从 C 向前声明匿名结构

c - 邻居发现 C