1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Functions stored in structure

Discussion in 'C' started by boschow, Oct 8, 2007.

  1. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,
    it is possible to declere a function within a structure, then in the main function with the use of the pointers call this function and assign the specific parameters . To make my idea clearer check out the sample code.
    Code:
    struct {
           int AND_function;
           int math_funct;
           int comp_funct;
           int regul_funct;
    }functions;
    
    AND_function (parameter1, parameter2, . . .)
    {
                         .
                         .
                         .
    }
    
    int main()
    {
         struct functions *fun;
         function.AND_function(par.parameter1, par.parameter);
    }
    
    Thanks for your help,
    Best regards,
    BoSCHoW.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    As I can understand what you are looking for is possible in C++ but not the way you have written it.
     
  3. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Is this possible only in C++ and not in C. If its possible in C could you write some examples for both program languages.

    Thanks,
    Best regards,
    BoSChoW.
     
  4. buddy

    buddy New Member

    Joined:
    Aug 31, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    No it is not possible to declare a function inside a Structure using c,
    Only in c++ it is possible.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    You should really specify whether you are talking about C or C++. Some people will tell you that C++ is a superset of C, but that isn't true. Structs are one of the notable exceptions.

    You may define function pointers in C, and try to restrict their locale to a struct, but there are no guarantees that those who follow will adhere to your strictures.
     

Share This Page