468,513 Members | 984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 60410
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 this site, you agree to our Privacy Policy and Terms of Use.