Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aly

macrumors member
Original poster
Jul 3, 2006
88
0
Scotland
Hi everyone,

I know Objective-C allows you to use C functions as it is just an extension of C but is it bad practice to do so? I just don't often see it in tutorials (if ever) and searching the topic brings up few results.

Part of the reason I ask is I have a function that takes in several arguments, one of which is a array of floats that has an indeterminate size before runtime. When I wrote this as a ObjC method:
Code:
-(float)MyFunctionWitha: (int) a andB (float) b[]
it refuses to compile due to using b[] it would seem. However writing as a C function:
Code:
float MyFunction(int a, float b[])
it compiles fine.

Any ideas?

Cheers,

Aly
 
Probably because your type declared for the second param is float and the name of that param is b[]. The characters [ ] (and probably others) are not allowed in param names. I'd have thought that

Code:
-(float)MyFunctionWitha: (int) a andB (float[]) b

would be allowed (although I've not tried it).
 
Thanks guys. The colon was a typo on my part and that function was just for an example. Got it working now using (float[]) b notation.

Thanks again,

Aly
 
You could also try:

Code:
- (CGFloat)myFunctionWithA:(NSInteger)a andB:(CGFloat *)b

and just pass a pointer to your C array.

That's what you would do if you want to pass in an array of floating point numbers meant for being processed by Core Graphics. If you want to pass an array of low precision floating point numbers, you pass float*. For high precision numbers, pass double*. For very high precision numbers, pass long double*.

On the other hand, you should only use single precision numbers (float) if you have a good reason to use them. If you can't give a reason why you should use float instead of one of the other types then double is usually the right choice.
 
On the other hand, you should only use single precision numbers (float) if you have a good reason to use them. If you can't give a reason why you should use float instead of one of the other types then double is usually the right choice.

I hadn't thought about it that much. I noticed that CGFloat typedef's to a double with my 64-bit Mac (and goes to a float for a 32-bit Mac).

Also, the OP didn't make it clear that the Apple frameworks were being used; it might have been inappropriate to assume they would have access to NSInteger and CGFloat.
 
Thanks for all your comments guys. I've got my methods working now and in fact moved from float to double now after reading the comments. So used to working with floats in work (due to low precision requirement) that I just went straight for that.

I'm working on a simple NURBS surface program for designing / optimising boat hulls. Its a bit of a pet project that stems from a group project I did at uni to optimise a hull form. It was done in excel (actually quite incredible considering what we achieved) because it was the only common "language" we all had. Now I want to increase the efficiency and bring it to the mac.

Fun times :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.