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

  • Home
  • SEARCH
  • 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 6251915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:42:05+00:00 2026-05-24T13:42:05+00:00

I have a C struct that contains a function pointer. Now, I have used

  • 0

I have a C struct that contains a function pointer. Now, I have used this setup within C with no problems, but now I’m using this C struct in Objective-C and I need to pass a function (or selector) pointer that is defined in the Objective-C class.

1. Here is what I have for the Objective-C selector that needs to be passed as a pointer to the C function:

- (void)myObjCSelector:(int*)myIntArray
{
    // Do whatever I need with myIntArray
}

2. And here is where I run into a wall, Within Objective-C I’m trying to pass the selector as a pointer to the C function call: In place of “myObjCSelectorPointer” I need the proper syntax to pass the selector as a function pointer in this C function call:

passObjCSelectorPointerToCContext(cContextReference, myObjCSelectorPointer);

I did investigate this issue, but could mainly find several different ways of doing similar things, but I couldn’t find anything specific for calling C functions and passing an Objective-C selector pointer.

  • 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-24T13:42:06+00:00Added an answer on May 24, 2026 at 1:42 pm

    In objc a selector is not a function pointer. A selector is a unique integer that is mapped to a string in a method lookup table stored by the objc runtime. In the above case your method name would be myObjCSelector: and to get the unique selector for it you would type @selector(myObjCSelector:). However this would be of no use to you because it doesnt represent a particular implementation of a function.

    What youre looking for is IMP. Refer to this SO question.

    EDIT 2:

     IMP myObjCSelectorPointer = (void (*)(id,SEL,int*))[self methodForSelector:@selector(myObjCSelector:)];
    

    Then you can call the method using

    myObjCSelectorPointer(self,@selector(myObjCSelector:),myIntArray);
    

    However, what this means you will need to make sure that you add the pointer to self in the c function call passObjCSelectorPointerToCContext.
    So it should look like this

    passObjCSelectorPointerToCContext(cContextReference, self, myObjCSelectorPointer); 
    

    when called from within the object that contains the method.

    It is important to note though that using IMP is almost never the right technique. You should try to stick with pure Obj-C. Obj-C is quite efficient after the first call to a message because it uses temporal caching.

    EDIT 1:

    It’s useful to understand why objc works in this way. The Apple documents explain it in depth. However a short explanation is as follows:

    When you send a message to an object such as [myobject somemethod] the compiler won’t immediately know which particular implementation of somemethod to call because there might be multiple classes with multiple overriden versions of somemethod. All of those methods have the same selector, irrespective of its arguments and return values and hence the decision about which implementation of somemethod is deffered to when the program is running. [myobject somemethod] gets converted by the compiler into a C function call:

    objc_msgSend(myobject, @selector(somemethod))
    

    This is a special function that searches each myobject class layout to see whether that class knows how to respond to a somemethod message. If not it then searches that class’s parent and so on until the root. If none of the classes can respond to somemethod then NSObject defines a private method called forward where all unknown messages are sent.

    Assuming that a class can respond to the somemethod message then it will also have a particular pointer of type IMP that points to the actual implementation of the method. At that point the method will be called.

    There is considerably more to this procedure than I have described but the outline should be enough to help you understand what the goal of a selector is.

    One final point is that the reason method names are mapped to unique integers via the @selector directive is so that the runtime doesn’t have to waste time doing string comparisons.

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

Sidebar

Related Questions

I have a function that takes pointer to pointer to struct. struct ABC; void
I have a dll which accepts a struct that contains a pointer to a
I have a function that takes a struct, and I'm trying to store its
I have this routine that calculates the seconds-to-date for a struct tm . On
I have class with a member function that takes a default argument. struct Class
I have an LLVM (version 2.7) module with a function that takes a pointer
I have this comparator function for my qsort in C, but I seem to
I have a struct that's used in my internet application, which looks like: struct
Using MSVC2010... I have a struct that wraps a std::string, with standard move ctor's
I have an unmanaged dll that contains a function to read a data from

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.