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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:54:01+00:00 2026-06-13T03:54:01+00:00

I am not new to C programming. But I don’t understand why keeping a

  • 0

I am not new to C programming. But I don’t understand why keeping a pointer to a function as a structure member is useful in C. Example:

    // Fist Way: To keep pointer to function in struct
    struct newtype{
        int a;
        char c;
        int (*f)(struct newtype*);
    } var;
    int fun(struct newtype* v){
        return v->a;
    }
    
    // Second way: Simple
    struct newtype2{
        int a;
        char c;
    } var2;
    int fun2(struct newtype2* v){
        return v->a;
    }
            
    int main(){
    
        // Fist: Require two steps
        var.f=fun;
        var.f(&var);
        
        //Second : simple to call
        fun2(&var2);    
    }

Do programmers use it to give an Object-Oriented (OO) shape to their C code and provide abstract objects, or just to make code look technical?

I think that in the above code the second way is more gentle and pretty simple too. In the first way, we still have to pass &var, even though fun() is member of the struct.

If it’s good to keep function pointers within a struct definition, kindly explain the reason.

  • 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-13T03:54:03+00:00Added an answer on June 13, 2026 at 3:54 am

    Providing a pointer to function on a structure can enable you to dynamically choose which function to perform on a structure.

    struct newtype{
        int a;
        int b;
        char c;
        int (*f)(struct newtype*);
    } var;
    
    
    int fun1(struct newtype* v){
            return v->a;
        }
    
    int fun2(struct newtype* v){
            return v->b;
        }
    
    void usevar(struct newtype* v) {
       // at this step, you have no idea of which function will be called
       var.f(&var);
    }
    
    int main(){
            if (/* some test to define what function you want)*/) 
              var.f=fun1;
            else
              var.f=fun2;
            usevar(var);
        }
    

    This gives you the ability to have a single calling interface, but calling two different functions depending on if your test is valid or not.

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

Sidebar

Related Questions

First, i'm new at Java-programming and my native lang is not english, but still
I'm new to Java but not to programming. I'm reading the book Beginning Android
I'm new to Django and Web programming in general. Have googled but could not
I'm not sure whether these are programming questions, but I'm sure lots of new
Let me start off by saying, I'm not new to programming but am very
I'm not new to programming but I am new to Java. I have an
Pardon my ignorance, I'm new to Ruby, but not programming. I'm using a gem
I am new to programming. I don't understand what's wrong. This code always returns
I'm not new to programming, but I am new to game programming. It was
I'm new to programming and some OOP concepts and am not sure what is

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.