Thread: Typdef struct and function pointers

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    Typdef struct and function pointers

    Say I have a function that uses a typdef struct as a parameter:

    Code:
    function( mytype1 );
    
    function( *mytype )
    {
       /*do something to the specific variable*/
    }
    Is there any way I can make the function appear as if it is part of the struct with a function pointer, BUT not have to pass the parameter and have the function know which variable i am using?

    Code:
    mytype1->function(); //no parameter, can it find out which variable i want to manipulate?
    
    function()
    {
    
    }
    
    typdef struct
    {
        //function pointer
    }mytype;

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tempster09 View Post
    Code:
    mytype1->function(); //no parameter, can it find out which variable i want to manipulate?
    If there is only one possible answer, then yes it can. If there is more than one possibility, what do you think the answer is here?

    BTW, if you are doing this to mimic method calls, DON'T. That is not the purpose of function pointers; function pointers are for situations where you may substitute one function for another. Using them just so you can use method syntax is stupid, because:
    1) this is less efficient than calling a function directly -- which is why I said before, adherring to an OO model just for it's own sake will lead to unnecessary inefficiencies such as excess memory usage, excessive indirection, etc.
    2) the code ends up being less comprehensible (not more) in exact relation to it's decrease in efficiency, because you have actually given the processor unnecessary instructions to do unnecessary things, and those unnecessary directives are just clutter.

    Once again, the normative and quite common approach to OO in C is to make the "methods" external but name them in relation to the "class" they operate on:
    Code:
    typedef struct banana;
    void banana_dothis(banana *ptr);
    void banana_dothat(banana *ptr);
    Yes, unfortunately that means you cannot have a instance of banana and do this:
    Code:
    banana realbanana;
    realbanana.dothat();
    Instead, you must resort to the arcane and ridiculous
    Code:
    banana_dothat(&realbanana);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM