Struct based functions

I'm using a struct to told two integers. Is it possible to declare a function with that struct, so that it returns a struct of that type? (With the two integers.) (Sort of like how an int function returns a number, a char - a word, and a void - nothing.

This is what I mean:

Code:

struct foo {
int foo1;
int foo2;
}

foo funcBasedOnStruct() {

foo myFoo;

return myFoo;
}

Also, what if I wanted that same function to return an *array* of structs?

Taxxodium Wrote:And who told you that?? C is still much nicer than C++, although C++ has more functionality and you'll probably do OK if you don't use the nasty features of C++

To each their own. I wouldn't write anything non-trivial ( read: more than 1 kloc ) in C, unless I were paid to do so. I won't say "I know C++", or even "I am good at C++". It's simply too huge. But I love what I can do with it.

Taxxodium Wrote:Still, the best language for me is Objective C, but that's a matter of opinion...

If ObjC gave me operator overloading, templates, and lightweight classes with inlined non-virtual methods, I'd agree. But it doesn't, so for me, ObjC is where the logic goes, and C++ does the work.

Jones Wrote:...I'm using a struct to told two integers. Is it possible to declare a function with that struct, so that it returns a struct of that type? (With the two integers.) (Sort of like how an int function returns a number, a char - a word, and a void - nothing.

This is what I mean:

Code:

struct foo {
int foo1;
int foo2;
}

foo funcBasedOnStruct() {

foo myFoo;

return myFoo;
}

Also, what if I wanted that same function to return an *array* of structs?

Back to the original topic. The function you defined for returning foo is fine. It should work. The only issue that you might want to be aware of is that the entire struct will be copied to the stack so that it can be returned to the caller. If your struct is large this could slow things down if this function is called a lot. An alternative way is to have the function manipulate a struct passed to it instead of just returning it as you have done.

As someone else pointed out in their code but not really mentioned it. typedef your structure for less typing. Otherwise you will have to put in "struct foo" for every use of that struct.

All that is passed to the function is a pointer as oppose to a larger structure returned by function you wrote. Ok, in this case the cost is possibly insignificant and nothing to worry about but something to keep in mind if this becomes speed critical.

/* A different way without dynamic memory but you better know what's happening here.
** In this case there is only one array in the whole program. I wouldn't really recommend
** doing this though. I'd say, stay away from static variables in functions as much as
** you can for sanity sake.*/
foo *funcReturningStaticArrayOfFoo(void) {
static foo fooArray[arraySize];

fooArray[x].foo1 = ...;
...
return fooArray;
}

/* Or do the allocation somewhere else and keep the function as to the point as possible.*/
void funcModingArrayOfFoo(foo *fooArray) {