在C函数返回结构 [英] Returning struct in a c function

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

问题描述

我有类似如下因素C code:

I have something like the folowing C code:

struct MyStruct MyFunction (something here)
{
    struct MyStruct data;

    //Some code here

    return data;
}

将返回值是一个参考或者用于数据的存储块的副本?
如果MyFunction的收益结构MYSTRUCT *(与相应的内存分配),而不是结构MYSTRUCT?

would the returning value be a reference or a copy of the memory block for data? Should MyFunction return struct MyStruct* (with the corresponding memory allocation) instead of struct MyStruct?

推荐答案

这将返回一个副本。 C是一个传递价值的语言。除非您指定围绕传递指针,结构被复制的任务,return语句,并作为函数参数使用时。

It would return a copy. C is a pass-by-value language. Unless you specify that you are passing pointers around, structures get copied for assignments, return statements, and when used as function parameters.

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

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