c - Returning a struct from function -
i'm trying understand how return struct function i'm lost.
here 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 code .h
struct data{ char host[16]; char port_inc[8]; }; void initialize_server(struct data host_port);
the function definition
your function definition uses void
, defines function returning nothing, therefore return
statement useless (will discuss later).
use of structures
also definition seems little broken. have written:
void initialize_server(struct data host_port) {
then in function write things data.host
. i'm assuming have struct
definition data
somewhere , host_port
variable of type struct data
.
to create variable of type struct some-struct-name
write struct some-struct-name your-variable-name
. in code have
struct data { char host[size]; port_inc[size]; }
this defines layout of new composite data type. type identified name data
. create variables of type declare them follows:
struct data my_new_data_struct; // data structure on stage of type struct data *my_new_data_struct; // pointer data structure // allocated on heap
possible solutions
use structures passed value
you write function follows return structure, little inefficient, because returning structure having utilise stack return data , using stack pass in parameter function. called pass-by-value.
struct data initialize_server(struct data host_port)
then replace data.
bits 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 better pass in pointer data structure follows:
void initialize_server(struct data *const host_port)
this defines function initialize_server
returning nothing, won't need return anyway because not host_port
pointer. function 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 note when call function must pass in pointer! might have like:
void do_something( ) { struct data my_host_port; //do stuff initialize_server( &my_host_port); // note: ampersand!! // stuff }
or might malloc()
host port follows.
void do_something( ) { struct data *my_host_port = malloc(sizeof(struct data)); //do stuff initialize_server(my_host_port); // note: no ampersand my_host_port pointer!! // stuff free(my_host_port); // note: must free() malloc() }
Comments
Post a Comment