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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:27:36+00:00 2026-06-15T16:27:36+00:00

I am new to C, from a Java background. If I have a struct

  • 0

I am new to C, from a Java background.

If I have a struct that is initialized on the fly with data that comes from functions inside of the struct definition, when do those functions get called? When does this code run? Is it just on the first reference to sample_struct_table[i]?

static struct sample_struct {
    int         command;
    int         (*foo)( obj1 *banana, int num);
} sample_struct_table[] = {
    {   .command    =   COMMAND_1,
        .foo =   function_name,
    },
    {   .command    =   COMMAND_2,
        .foo =   another_function_name,
    },
};

static int function_name(obj1 *banana, int num)
{
      // do stuff here
      // When does this get called?
}
  • 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-06-15T16:27:37+00:00Added an answer on June 15, 2026 at 4:27 pm

    Short answer: it gets called when the pointers in the array are called. For example:

    sample_struct_table[0].foo(...);

    (Obviously replacing the elipses with the parameters needed to call the function).

    Long answer: C can have a pointer to a function, which means that you can load shared object (.dll, .so, etc) and look for functions defined at runtime, then call those functions with out ever linking to them. This is very powerful. If you are creating a struct that contains pointers to your functions, that is really most useful only if you want to call a series of functions using a loop. I suppose if a part of the struct were an int, the functions could specify which function to call next, but I can’t imagine a scenario where this would be useful. If you’re familiar with Scala and have used it in a system, then this shouldn’t be hard to figure out, as Scala does this all the time.

    scala> 1 to 10 foreach println

    Here the foreach function accepts a function that accepts a single Int as a parameter. We are passing the println function (def println(x : Any) = …) as a parameter to foreach.

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

Sidebar

Related Questions

I am new to Haskell from a C++ and Java background. Occassionally, I have
I'm relatively new to services in java (coming from a C# background) and have
I am developer from java background , new to ruby on rails. What's my
I'm new to PHP but come from a Java background and I'm trying to
I am relatively new to Objective-C and I am coming from a Java background,
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I transitioned from Java 1.4(previous company) to Java 1.6 (new company). What I have
I have a Java process that runs in the background. How can I quickly
I'm new to C programming, I come from a Java background. I was wondering
I am fairly new to learning C# (from Java & C++ background) and I

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.