Struct based functions

Jones
Unregistered
 
Post: #1
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!
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #2
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));
*/

"When you dream, there are no rules..."
Quote this message in a reply
Jones
Unregistered
 
Post: #3
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.

Smile
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #4
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. Grin
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #5
Blacktiger Wrote:It's not, C is known for efficiency not fun. Grin

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...

"When you dream, there are no rules..."
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #7
Let's try not to turn this into yet another C vs. C++ vs. Objective-C debate, shall we? Smile
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #8
ThemsAllTook Wrote:Let's try not to turn this into yet another C vs. C++ vs. Objective-C debate, shall we? Smile

Agreed, because we all know AppleScript beats 'em all Rasp

Besides, doesn't ObjC++ allow for operator overloading?

"When you dream, there are no rules..."
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #9
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.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #10
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);
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Functions not being initialised onKeyDown Yendall 1 1,291 Apr 15, 2014 01:31 PM
Last Post: OneSadCookie
  Malloc() Struct with NSString Inside? Graphic Ace 3 4,829 Jan 26, 2010 05:32 PM
Last Post: cmiller
  Some quick help getting started with certain Carbon functions zmwworm 12 8,259 Jan 10, 2008 01:14 AM
Last Post: zmwworm
  Intel Mac -> Many Warnings (deprecated functions) dave05 3 4,622 Sep 6, 2006 03:19 PM
Last Post: aarku
  Arrays or variables containing executable functions Jones 4 4,887 Jun 2, 2006 08:35 AM
Last Post: Zekaric