从C中的函数返回一个`struct` [英] Return a `struct` from a function in C

查看:151
本文介绍了从C中的函数返回一个`struct`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我在教学上几个朋友如何用C 结构秒。其中一个人问,如果你能返回结构从函数,而我回答说:不,你会返回指针动态的malloc ED 结构!而非。

Today I was teaching a couple of friends how to use C structs. One of them asked if you could return a struct from a function, to which I replied: "No! You'd return pointers to dynamically malloced structs instead."

从别人谁做主要是C ++的到来,我期待不能通过值返回结构秒。在C ++中,你可以重载运算符= 为对象,使完整意义上具有通过价值来回报您的对象。在C,但是,你没有这样的选择,所以它让我思考什么编译器实际上做。考虑以下几点:

Coming from someone who primarily does C++, I was expecting not be able to return structs by values. In C++ you can overload the operator = for your objects and makes complete sense to have a function to return your object by value. In C, however, you do not have that option and so it got me thinking what the compiler is actually doing. Consider the following:

struct MyObj{
    double x, y;
};

struct MyObj foo(){
    struct MyObj a;

    a.x = 10;
    a.y = 10;

    return a;
}        

int main () {

    struct MyObj a;

    a = foo();    // This DOES work
    struct b = a; // This does not work

    return 0;
}    

我明白为什么结构B = A; 不应该工作 - 你不能超载运算符= 为您的数据类型。它是如何 A = foo的(); 编译罚款?这是否意味着比结构B以外的东西= A; ?也许要问的问题是:究竟是否收益语句一起 = 征办

I understand why struct b = a; should not work -- you cannot overload operator = for your data type. How is it that a = foo(); compiles fine? Does it mean something other than struct b = a;? Maybe the question to ask is: What exactly does the return statement in conjunction to = sign do?

:好吧,我只是指出结构B = A 是一个语法错误 - 这是正确的,我是个白痴!但是,这使得它更复杂!使用结构MyObj中B = A 确实工作!缺少什么我在这里?

[edit]: Ok, I was just pointed struct b = a is a syntax error -- that's correct and I'm an idiot! But that makes it even more complicated! Using struct MyObj b = a does indeed work! What am I missing here?

推荐答案

您可以从一个函数返回一个结构(或使用 = 运营商)没有任何问题。这是一个良好定义的语言的一部分。与结构B = A 唯一的问题是,你没有提供一个完整的类型。 结构MyObj中B = A 将工作得很好。你可以通过结构的的功能,以及 - 一个结构完全一样,任何内置类型参数传递的目的,返回值,并赋值

You can return a structure from a function (or use the = operator) without any problems. It's a well-defined part of the language. The only problem with struct b = a is that you didn't provide a complete type. struct MyObj b = a will work just fine. You can pass structures to functions as well - a structure is exactly the same as any built-in type for purposes of parameter passing, return values, and assignment.

下面是做所有三个简单的演示程序 - 传递一个结构作为参数,从一个函数返回一个结构,并使用结构赋值语句:

Here's a simple demonstration program that does all three - passes a structure as a parameter, returns a structure from a function, and uses structures in assignment statements:

#include <stdio.h>

struct a {
   int i;
};

struct a f(struct a x)
{
   struct a r = x;
   return r;
}

int main(void)
{
   struct a x = { 12 };
   struct a y = f(x);
   printf("%d\n", y.i);
   return 0;
}

下一个例子是pretty多少完全一样,但使用内置的 INT 键入演示目的。两个方案都针对相同的行为通过按值参数传递,转让等。

The next example is pretty much exactly the same, but uses the built-in int type for demonstration purposes. The two programs have the same behaviour with respect to pass-by-value for parameter passing, assignment, etc.:

#include <stdio.h>

int f(int x) 
{
  int r = x;
  return r;
}

int main(void)
{
  int x = 12;
  int y = f(x);
  printf("%d\n", y);
  return 0;
}

这篇关于从C中的函数返回一个`struct`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆