从函数返回结构 [英] Returning a struct from function

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

问题描述

我试图了解如何从函数返回结构,但是我完全迷失了.

I'm trying to understand how to return a struct from a function but I'm completely lost.

这是代码:

void initialize_server(struct data host_port){

        printf(" Set IP Address (X.X.X.X): ");
        fgets(data.host, sizeof(data.host), stdin);
        strchomp(data.host);

        printf(" Set Port: ");
        fgets(data.port_inc, sizeof(data.port_inc), stdin);
        strchomp(data.port_inc);

        return data;
}

这是.h中的代码

struct data{
    char host[16];
    char port_inc[8];
};

void initialize_server(struct data host_port);

推荐答案

函数定义

您的函数定义使用 void ,该函数将函数定义为不返回任何内容,因此您的 return 语句是无用的(稍后将对此进行讨论)

The function definition

Your function definition uses void, which defines the function as returning nothing, therefore your return statement is useless (will discuss this later).

这个定义似乎也有点混乱.您已写过:

Also the definition seems a little broken. You have written:

void initialize_server(struct data host_port) {

然后在您的函数中编写诸如 data.host 之类的内容.我假设您在某处具有 data struct 定义,并且 host_port struct data 类型的变量

Then in your function you write things like data.host. I'm assuming that you have a struct definition for data somewhere and host_port is the variable of type struct data.

要创建类型为 struct some-struct-name 的变量,您可以编写 struct some-struct-name your-variable-name .所以在您的代码中您可以拥有

To create a variable of type struct some-struct-name you would write struct some-struct-name your-variable-name. So in your code you could have

struct data {
    char host[SIZE];
    port_inc[SIZE];
}

定义新的复合数据类型的布局.类型由名称 data 标识.要创建此 type 类型的变量,您可以按以下方式声明它们:

This defines the layout of your new composite data type. The type is identified by the name data. To create variables of this type you would declare them as follows:

struct data my_new_data_struct;  // A data structure on the stage of type
struct data *my_new_data_struct; // A pointer to data structure that 
                                 // would normally be allocated on the heap

可能的解决方案

使用按值传递的结构

您可以按以下方式编写函数以返回结构,但这会有点效率低下,因为通过返回结构,您可能不得不利用 pass-按值 .

Possible solutions

Use structures passed by value

You could write your function as follows to return the structure, but it will be a little inefficient, because by returning a structure you are likely to be having to utilise the stack to return the data and you are also using the stack to pass in the parameter to the function. This is called pass-by-value.

结构数据initialize_server(结构数据host_port)

struct data initialize_server(struct data host_port)

然后将所有 data.位替换为 host_port.:

void initialize_server(struct data host_port)
{
        printf(" Set IP Address (X.X.X.X): ");
        fgets(**host_port**.host, sizeof(**host_port**.host), stdin);
        strchomp(**host_port**.host);

        printf(" Set Port: ");
        fgets(**host_port**.port_inc, sizeof(**host_port**.port_inc), stdin);
        strchomp(**host_port**.port_inc);

        return **host_port**;
}

使用指针

更好的方法是按如下所示传递指向数据结构的指针:

Use pointers

What might be better is to pass in a pointer to the data structure as follows:

void initialize_server(struct data *const host_port)

这将函数 initialize_server 定义为不返回任何内容,因此您无需返回,因为 host_port 不是指针.该函数将变为:

This defines the function initialize_server as returning nothing, so you won't need to return anyway because not host_port is a pointer. The function would then become:

void initialize_server(struct data *const host_port)
{
        printf(" Set IP Address (X.X.X.X): ");
        fgets(data.host, sizeof(host_port->host), stdin);
        strchomp(host_port->host);

        printf(" Set Port: ");
        fgets(host_port->port_inc, sizeof(host_port->port_inc), stdin);
        strchomp(host_port->port_inc);
}

但是现在请注意,当您调用此函数时,必须传递一个指针!因此,您可能会遇到类似这样的情况:

But now note that when you call this function you must pass in a pointer! So you might have something like:

void do_something( )
{
   struct data my_host_port;
   //do stuff
   initialize_server( &my_host_port); // NOTE: The ampersand!!
   // do stuff
}  

或者您可以如下所示 malloc()您的主机端口.

Or you might malloc() your host port as follows.

void do_something( )
{
   struct data *my_host_port = malloc(sizeof(struct data));
   //do stuff
   initialize_server(my_host_port); // NOTE: NO ampersand as my_host_port  now already a pointer!!
   // do stuff
   free(my_host_port); // NOTE: You must free() what you malloc()
}  

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

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