PDA

View Full Version : Using C Functions in Objective - C.




aly
Oct 5, 2009, 07:20 AM
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:
-(float)MyFunctionWitha: (int) a andB (float) b[]
it refuses to compile due to using b[] it would seem. However writing as a C function:
float MyFunction(int a, float b[])
it compiles fine.

Any ideas?

Cheers,

Aly



robbieduncan
Oct 5, 2009, 07:57 AM
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


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


would be allowed (although I've not tried it).

whooleytoo
Oct 5, 2009, 09:42 AM
You're missing the colon after the andB too? i.e.


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

robbieduncan
Oct 5, 2009, 09:47 AM
You're missing the colon after the andB too? i.e.


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


That's what I get for typing code straight into the reply box and not previewing it :p

whooleytoo
Oct 5, 2009, 09:49 AM
That's what I get for typing code straight into the reply box and not previewing it :p

It was the same in the original post. :)

(I don't even check my code for errors any more. I just post it on here and the sharks will find errors that Xcode would never spot.. :p )

aly
Oct 5, 2009, 03:11 PM
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

North Bronson
Oct 6, 2009, 01:37 AM
You could also try:

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

and just pass a pointer to your C array.

gnasher729
Oct 6, 2009, 05:33 AM
You could also try:

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

North Bronson
Oct 6, 2009, 01:06 PM
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.

aly
Oct 6, 2009, 03:41 PM
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 :)