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 7441065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:57:04+00:00 2026-05-29T10:57:04+00:00

Scenario: There are multiple C structs, each of which contains a function pointer to

  • 0

Scenario:

There are multiple C structs, each of which contains a function pointer to the same function. These pointers can be different if necessary (pointers to pointers, etc.), but must all point, eventually, to the same function.

Problem:

When the function is called from one of the pointers, I need to retrieve, within the function, which struct it was called from.

e.g.

typedef struct A {
    void * (*func)();
    ... /* Custom properties */
} * A;

typedef struct B {
    void * (*func)();
    ... /* Custom properties */
} * B;


A a_init() {
    A a;
    ... /* Custom initialisation, allocation, etc. */
    a->func = myFunc;
    return a;
}

B b_init() {
    B b;
    ... /* Custom initialisation, allocation, etc. */
    b->func = myFunc;
    return b;
}


int main () {
    A a = a_init();
    void *something = a->func();
}


void * myFunc () {
    // Need to get pointer to the instance of the struct this was called from here
}

Is there any way I can retrieve a pointer to the caller within myFunc? If necessary, I was thinking of creating pointers to pointers, etc. to the function, so each instance of an object would have a different pointer, and store all of them in a central location to match them up, but that obviously won’t work if I can’t even find the instance of the object or the pointer which was used. Any ideas?

Edit:

The question was intended a bit more broadly than I seem to have put it. Currying would be a great solution, if anyone has any ideas as to how to implement it in C. I had some ideas, but I just ended up coming right back to this spot with it.

  • 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-29T10:57:05+00:00Added an answer on May 29, 2026 at 10:57 am

    I don’t think there is any way within the language to do that, without explicitly passing some identifier (as an argument or a global) to myFunc. The address of a_init() exists somwhere within the call stack, but it’s not accessible from the program.

    It’s like if somebody showed up at your door unannounced, how would you find out where that person came from without asking?

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

Sidebar

Related Questions

In this scenario (your basic catalog), there are multiple <div> s, each containing an
Scenario: There are two entities say 'Books' and 'Book Reviews'. There can be multiple
I have a scenario where there are multiple mysql 4.x servers. These databases were
I have a scenario where there are multiple dbs that have the same schema
I have a scenario in which there are multiple iframes/frames open in one web
I have a scenario. (Windows Forms, C#, .NET) There is a main form which
Scenario: We have a server where there are multiple ASP.NET websites hosted on it.
I have a scenario where I have multiple versions of the same assembly that
Scenario: The system has number of components, each with its own POM. There are
Scenario: There exists 'n' teams who each work on their virtual 'wall' (like facebook's

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.