Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 594257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:56:03+00:00 2026-05-13T15:56:03+00:00

In an Xcode project I have a C file with functions, it compiles and

  • 0

In an Xcode project I have a C file with functions, it compiles and works OK

I want to wrap my C code in struct(s), how will I be able to call them in Objective-C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:56:03+00:00Added an answer on May 13, 2026 at 3:56 pm

    Declare function pointers, add them to your structure and then call them, it’s just C.

    Example:

    //Typedef 2 function pointers, first takes and returns int,
    // second takes and returns double
    typedef int    (*FuncPtrInt)   (int);
    typedef double (*FuncPtrDouble)(double);
    
    // create structure to store function pointers
    struct ABC
    {
        FuncPtrInt    applyA;
        FuncPtrDouble applyB;
    };
    
    // create some functions to use with structure
    int incrFuncA(int num) { return ++num; }
    double decrFuncB(double num) { return --num; }
    double multiplyFuncB(double num) { return num*num; }
    
    // try it out
    void testStruct()
    {
        struct ABC abc;
        abc.applyA = incrFuncA;
        abc.applyB = decrFuncB;
    
        NSLog(@"increment: %d",abc.applyA(3));
        NSLog(@"decrement: %f",abc.applyB(3.5));
    
        abc.applyB = multiplyFuncB;
    
        NSLog(@"multiply: %f",abc.applyB(3.5));
    }
    

    Output:

    2010-02-01 10:36:22.335 x[11847] increment: 4
    2010-02-01 10:36:22.336 x[11847] decrement: 2.500000
    2010-02-01 10:36:22.336 x[11847] multiply: 12.250000
    

    If you want to have a struct with functions where functions operate on the structure you have to pass the pointer to that function by default (similar to what c++ does):

    Define:

    struct ClassABC;
    typedef int (*FuncPtrClassABC)(struct ClassABC *);
    typedef int (*FuncPtrClassABCInt)(struct ClassABC *, int);
    
    int incrFunc(struct ClassABC * abc);
    int decrFunc(struct ClassABC * abc);
    int addFunc(struct ClassABC * abc, int num);
    int subtractFunc(struct ClassABC * abc, int num);
    
    struct ClassABC
    {
        int i;
        FuncPtrClassABC    increment;
        FuncPtrClassABC    decrement;
        FuncPtrClassABCInt add;
        FuncPtrClassABCInt subtract;
    };
    

    As you can see these functions could be standalone, you would still pass the ClassABC in:

    int incrFunc(struct ClassABC * abc) { return ++(abc->i); }
    int decrFunc(struct ClassABC * abc) { return --(abc->i); }
    int addFunc(struct ClassABC * abc, int num)
    { abc->i += num; return abc->i; }
    int subtractFunc(struct ClassABC * abc, int num)
    { abc->i -= num; return abc->i; }
    

    Initialization helper func:

    void initClassABC(struct ClassABC * abc)
    {
        abc->i = 0;
        abc->increment = incrFunc;
        abc->decrement = decrFunc;
        abc->add = addFunc;
        abc->subtract = subtractFunc;
    }
    

    Usage:

    struct ClassABC cabc;
    initClassABC(&cabc);
    
    cabc.add(&cabc,4);
    NSLog(@"add: %d", cabc.i);
    
    cabc.decrement(&cabc);
    NSLog(@"decrement: %d", cabc.i);
    
    cabc.subtract(&cabc,2);
    NSLog(@"subtract: %d", cabc.i);
    

    Output:

    2010-02-01 10:56:39.569 x[12894] add: 4
    2010-02-01 10:56:39.569 x[12894] decrement: 3
    2010-02-01 10:56:39.569 x[12894] subtract: 1
    

    Enjoy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer uploadify uses flash to perform the upload. Flash is subject… May 15, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer Sorry, but I can't see how can I get MyBusinessObject… May 15, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer You need to use response.content rather than just response in… May 15, 2026 at 1:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.