471,873 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

can functions return a struct?

I am just asking if i can return the values of a struct on a function??
example:
typedef struct sample{
int a;
char b, c;
}SAMPLE_STRUCT;

can it be like this?

//function prototype
SAMPLE_STRUCT copy_struct(void);

Is this correct???
Sep 1 '06 #1
3 60649
It is possible. In header file:

typedef struct sample
{
int a;
char b, c;
}
SAMPLE_STRUCT;
//////
SAMPLE_STRUCT GetSample();

************************************
In source file:

SAMPLE_STRUCT GetSample()
{
SAMPLE_STRUCT sample_struct;

sample_struct.a = 5;
sample_struct.b = 6;
sample_struct.c = 7;

return sample_struct;
}

Also GetSample may the member function of some class, but in this case the structure is must be visible for this class.
Sep 1 '06 #2
rgb
37
you can also pass your structure as a pointer parameter in your function to make changes in the variables inside the sturcture.
Sep 1 '06 #3
It is possible. In header file:

typedef struct sample
{
int a;
char b, c;
}
SAMPLE_STRUCT;
//////
SAMPLE_STRUCT GetSample();

************************************
In source file:

SAMPLE_STRUCT GetSample()
{
SAMPLE_STRUCT sample_struct;

sample_struct.a = 5;
sample_struct.b = 6;
sample_struct.c = 7;

return sample_struct;
}

Also GetSample may the member function of some class, but in this case the structure is must be visible for this class.


PLZ. this is must be visible in the same class, if I want see struct variables in the other class.


Thank you in advance.
May 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.