Returning a struct from function

你说的曾经没有我的故事 提交于 2021-02-08 09:49:17

问题


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

Here is the code:

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;
}

and heres the code from the .h

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

void initialize_server(struct data host_port);

回答1:


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).

Use of structures

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

void initialize_server(struct data host_port) {

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.

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];
}

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

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.

struct data initialize_server(struct data host_port)

Then replace all data. bits with 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)

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
}  

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()
}  



回答2:


Some problems in the code:

  • The return type of your function is void, which means: return nothing. This has to be struct data.

  • Inside your function host_port.host has to be used instead of data.host.

  • You'll need to return host_port, not data (struct data is the type, host_port is the name of the parameter).

Solution:

First, it's easier to use a typedef for struct data:

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

Now, data can be used as the type instead of struct data.

data initialize_server(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;
}

Or even use a pointer to struct data as parameter, to avoid copying the whole struct:

void initialize_server(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);
}

This can be called as follows:

data dt;
initialize_server(&dt); // takes the address of dt

On return, dt will contain the modified values.




回答3:


First of all, data is name of struct, just like int is a name of type. And host_port is a name of a variable. So use host_port instead of data where possible Secondly, the return type is void and not struct data. Change it too.

struct data 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(data.port_inc), stdin);
        strchomp(host_port.port_inc);

        return host_port;
}

Also consider writing

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

As far as you set all fields of structure in your function, you can remove a parameter from function:

struct data 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);
//...

Or pass it by pointer and modify inside your function.




回答4:


Here is a struct :

struct a_tag {
   char c;
   int i;
   char *w;
};

You can declare a function that returns a struct like this :

struct a_tag  return_struct(void);

Then, you can write something like this :

int main() {
   struct a_tag a1;
   a1 = return_struct();
   return 0;
}

And you have define your function like this :

/* return_struct: return a struct by value (copied) */
struct a_tag return_struct(void) {
   struct a_tag a;
   a.c = 'Y';
   a.i = 88;
   a.w = "My dog has fleas";
   /* can return local auto storage, because it is copied */
   return a;
}

That's the basic concept around functions returning struct. Hope it helps.




回答5:


First of All,in order to return a variable from a function,the return type of the function has to be similar to the type of the variable.so a function of type void can't return anything.Since you want to return a struct,change the prototype of your function to:struct data initialize_server(struct data host_port); Next,you can't return a type,but the object created by that type.

So write return host_port;



来源:https://stackoverflow.com/questions/34825868/returning-a-struct-from-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!