Using C Functions in Objective - C.

Discussion in 'Mac Programming' started by aly, Oct 5, 2009.

  1. aly
    macrumors member

    Joined:
    Jul 3, 2006
    Location:
    Scotland
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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).
     
  3. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #3
    You're missing the colon after the andB too? i.e.

    Code:
    -(float)MyFunctionWitha: (int) a andB: (float[]) b
    
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That's what I get for typing code straight into the reply box and not previewing it :p
     
  5. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #5
    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 )
     
  6. aly
    thread starter macrumors member

    Joined:
    Jul 3, 2006
    Location:
    Scotland
    #6
    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
     
  7. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #7
    You could also try:

    Code:
    - (CGFloat)myFunctionWithA:(NSInteger)a andB:(CGFloat *)b
    and just pass a pointer to your C array.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    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.
     
  9. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #9
    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.
     
  10. aly
    thread starter macrumors member

    Joined:
    Jul 3, 2006
    Location:
    Scotland
    #10
    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 :)
     

Share This Page