Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I return a struct from a function? 2

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
I want to return a structure from a function instead of declaring the structure at the beginning. Can I do that? example:

// begin code
/*
should I declare struct here? I prefer to defining
in the function because I may not use that function depending on situation. Am I violating the scope of the structure?
*/
// function declaration
struct *ret(int a, int b);

main()
{
// do something on struct elements. e.g.
printf("%d\n", ret.value);

}

struct *ret(int, int)
{
typedef struct {
int value;
int index;
} s1;
/* or can I use struct
struct s1 {
int value;
int index;
};
*/
struct s1 ret;
ret.value = a;
ret.index = b;

return ret;
}
// end code

Or what if I use this code?
struct s1 {
int value;
int index;
}

// is this declaration correct?
struct *ret(int a, int b);

main()
{
struct s1 s;
// do something on struct s
printf("%d\n",s.value);
}

struct *ret(int a, int b)
{
struct s.value = a;
struct s.index = b;
}
// end code

If both of them are not correct, please tell me the correct way to return and pass a structure. Any small sample is good help. Thx.
 
Try this

#include <stdio.h>

struct t_s {
int a;
int b;
};


void func(struct t_s *p)
{
p->a = 30;
p->b = 40;
}

int main()
{
struct t_s node;

node.a = 10;
node.b = 20;

printf(&quot;Element of node is %d %d\n&quot;, node.a, node.b);

func(&node);

printf(&quot;Element of node is %d %d\n&quot;, node.a, node.b);

return 0;
}
 
Then this is my second way to do that. But is it possible to declare the structure inside func()? I guess no because of scope problem. Any reference?
 
I always use the declare with Keyword, easier to see and understand.

typedef struct {
int val;
int num;
char *s;
char d[512];
} MYSTRUCT;

then
MYSTRUCT data;
MYSTRUCT *dataptr;



Then to return: return &data;
return dataptr;

will do it. Keep in mind if the 1st is declared in a function, then it is local and the return is undefined. On some compilers it actually works.
 
Here is an example.

main()
{
MYSTRUCT *data;
data = myfunc();
}

MYSTRUCT *myfunc()
{
MYSTRUCT *d;
d = (MYSTRUCT *)calloc(1,sizeof(MYSTRUCT));
if (!d) return NULL;
return d;
}

Defining MYSTRUCT d is not correct as d is a local variable and when myfunc goes out of scope, d will no longer be valid.
You can do it this way.
main()
{
MYSTRUCT data;
myfunc(&data);
}

void myfunc(MYSTRUCT *d)
{
d->val = 56;
d->num = 100;
strcpy(d->d,&quot;This is text data&quot;);
return;
}
 
> But is it possible to declare the structure inside func()?
Yes, they have scope just the same as everything else has a scope.
So you can make them local to a function, local to a file, or global.

You need to put the structure in the place where it is in scope for all the things which need to refer to it.

--
 
Thanks, all of you. Esp. cdlvj's example shows exactly local vs. global struct usage. Again, all of you are big help.
 
You can return a structure declared within a function without any problems. When you return a structure a copy of the function's local structure is made before returning. So the deallocation that occurs when the function returns does not affect the values returned. For example:
[tt]
struct point makepoint(int x,int y)
{
struct point p;

p.x = x;
p.y = y;

return p;
}
[/tt]
However, if you are returning a pointer to the structure, you will have problems as the pointer will be addressing the structure deallocated when the function returns. In this case you should allocate memory dynamically using malloc (as shown by cdlvj). An alternative is to declare the structure as static (however there some potential problems with this).

Also it is more efficient to return a pointer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top