Struct based functions - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: Struct based functions (/thread-4289.html) Struct based functions - Jones - May 7, 2006 01:43 PM Pardon me for asking a, perhaps, "newbish" question, but my experience so far with C is not, shall we say, amazing yet. 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? Thanks for any help you can offer! Struct based functions - Taxxodium - May 7, 2006 01:50 PM Yes it's possible, for example Code: ```typedef struct { int x; int y; } PointRef; PointRef myPointRef() { return localPointRef; } PointRef* myPoints() { return localPoints; } /* localPoints = (PointRef*)malloc(20*sizeof(PointRef)); */``` Struct based functions - Jones - May 7, 2006 02:03 PM Taxxodium Wrote:Yes it's possible, for example Code: ```typedef struct { int x; int y; } PointRef; PointRef myPointRef() { return localPointRef; } PointRef* myPoints() { return localPoints; } /* localPoints = (PointRef*)malloc(20*sizeof(PointRef)); */``` Ok, thanks! I now know that's not that that's causing the errors in my code. Struct based functions - Blacktiger - May 8, 2006 04:16 AM Jones Wrote:Pardon me for asking a, perhaps, "newbish" question, but my experience so far with C is not, shall we say, amazing yet. Whoever told you the C experience was amazing? It's not, C is known for efficiency not fun. Struct based functions - Taxxodium - May 8, 2006 04:22 AM Blacktiger Wrote:It's not, C is known for efficiency not fun. 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++ Still, the best language for me is Objective C, but that's a matter of opinion... Struct based functions - TomorrowPlusX - May 8, 2006 12:47 PM 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. Struct based functions - ThemsAllTook - May 8, 2006 01:32 PM Let's try not to turn this into yet another C vs. C++ vs. Objective-C debate, shall we? Struct based functions - Taxxodium - May 8, 2006 01:47 PM ThemsAllTook Wrote:Let's try not to turn this into yet another C vs. C++ vs. Objective-C debate, shall we? Agreed, because we all know AppleScript beats 'em all Besides, doesn't ObjC++ allow for operator overloading? Struct based functions - TomorrowPlusX - May 8, 2006 02:49 PM Heh. My original post was going to request that we not get into Yet Another Language Argument. But then I lost control and argued for C++. Hulk Smash. Quote:Besides, doesn't ObjC++ allow for operator overloading? Yes, in that you can call C++ with operator overloading from ObjC. But you can't make an ObjC class and have operator overloading act on it. I use a lot of ObjC++. Honestly, for writing apps I LOVE ObjC. Struct based functions - Zekaric - May 9, 2006 02:37 PM 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. Code: ```/*typdefing for less typing typdef struct { int foo1; int foo2; } foo; /* passing a foo structure to the function to modify.*/ void funcModingFoo(foo *fooData) {    fooData->foo1 = ...;    ... } ... foo myFoo; /* Your way.*/ myFoo = funcBasedOnStruct(); /* Moding way.*/ funcModingFoo(&myFoo);``` 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. For arrays the following would do. Code: ```/* function returning an array of foo.  You need to use dynamic memory here.*/ foo *funcReturningArrayOfFoo(void) {    foo *fooArray;    fooArray = (foo *) malloc(sizeof(foo) * ArraySize);    fooArray[x].foo1 = ...; ...    return fooArray; } /* 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) {    fooArray[x].foo1 = ...; ... } ... foo *myFooArray; myFooArray = funcReturningArrayOfFoo(); myFooArray = funcCreatingFooArray(); funcModingArrayOfFoo(myFooArray);```