routines in structures ?

Discussion in 'C Programming' started by Skybuck Flying, Feb 9, 2008.

  1. Hello,

    Is it possible to declare routines in structures ?

    Something like:

    struct
    {
    int a, b, c

    void Test;

    } myStruct;

    void myStruct.Test()
    {
    a = 5;
    }

    Only a,b,c would be stored in the structure.

    This would allow to associate routines with structures a little bit better,
    just syntaxtically.

    Instead of writing:

    myStruct_Test( myStruct );

    I could for example write something like:

    myStruct.Test();

    Bye,
    Skybuck.
    Skybuck Flying, Feb 9, 2008
    #1
    1. Advertising

  2. In article <d80cc$47ae0e05$541983fa$1.nb.home.nl>,
    Skybuck Flying <> wrote:

    >Is it possible to declare routines in structures ?


    >Something like:


    >struct
    >{
    > int a, b, c


    > void Test;


    >} myStruct;


    >void myStruct.Test()
    >{
    > a = 5;
    >}


    No, this is not legal. What is legal is

    struct {
    int a, b, c;
    void (*Test)(void);
    } myStruct;

    void myStruct_Test(void) { myStruct.a = 5; }
    myStruct.Test = &myStruct_Test;

    --
    "Ignorance has been our king... he sits unchallenged on the throne of
    Man. His dynasty is age-old. His right to rule is now considered
    legitimate. Past sages have affirmed it. They did nothing to unseat
    him." -- Walter M Miller, Jr
    Walter Roberson, Feb 9, 2008
    #2
    1. Advertising

  3. "Skybuck Flying" <> wrote in message
    > Hello,
    >
    > Is it possible to declare routines in structures ?
    >
    > Something like:
    >
    > struct
    > {
    > int a, b, c
    >
    > void Test;
    >
    > } myStruct;
    >
    > void myStruct.Test()
    > {
    > a = 5;
    > }
    >
    > Only a,b,c would be stored in the structure.
    >
    > This would allow to associate routines with structures a little bit
    > better, just syntaxtically.
    >
    > Instead of writing:
    >
    > myStruct_Test( myStruct );
    >
    > I could for example write something like:
    >
    > myStruct.Test();
    >

    Probably you want C++.
    However use of function pointers can produce something very similar to a C++
    struct with function members. However you have to initialise each function
    pointer explicitly.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 9, 2008
    #3
  4. "Skybuck Flying" <> writes:

    > Is it possible to declare routines in structures ?
    >
    > Something like:
    >
    > struct
    > {
    > int a, b, c
    > void Test;
    > } myStruct;
    >
    > void myStruct.Test()
    > {
    > a = 5;
    > }


    No.

    > Instead of writing:
    >
    > myStruct_Test( myStruct );
    >
    > I could for example write something like:
    >
    > myStruct.Test();


    <OT>
    Interestingly, BCPL (a precursor to B and thus, in some way, C) has
    some syntax to assist with virtual method calls. The special kind of
    function call:

    FUNCTION#(E1, E2, ...)

    is taken to mean

    E1!0!FUNCTION(E1, E2, ...)

    which calls the function pointed to by the element numbered FUNCION in
    the zeroth element of the E1 vector (a!x is BCPL version of a[x]).

    This syntax was probably added after the split with B but it is
    interesting to speculate on how the world would have looked if it had
    been there before!
    </OT>

    --
    Ben.
    Ben Bacarisse, Feb 10, 2008
    #4
  5. Groovy hepcat Skybuck Flying was jivin' in comp.lang.c on Sun, 10 Feb
    2008 7:36 am. It's a cool scene! Dig it.

    > Is it possible to declare routines in structures ?


    If by routines you mean functions, then no. You can, however, declare
    pointers to functions in structures. For example:

    #include <stdio.h>

    struct somestruct
    {
    void (*somefunc)(void);
    };

    void thefunc(void)
    {
    puts("Hello, World!");
    }

    int main(void)
    {
    struct somestruct thestruct = {thefunc};
    thestruct.somefunc();
    return 0;
    }

    --
    Dig the sig!

    ----------- Peter 'Shaggy' Haywood ------------
    Ain't I'm a dawg!!
    Peter 'Shaggy' Haywood, Feb 12, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rich

    Proper serial routines ?

    Andrew Rich, Oct 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    443
    Al Tobey
    Oct 24, 2003
  2. Soenke
    Replies:
    0
    Views:
    558
    Soenke
    Dec 28, 2005
  3. Matthew Hood

    How to create global Funtions & Routines

    Matthew Hood, Jun 24, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,922
    Matthew Hood
    Jun 25, 2003
  4. tweak
    Replies:
    14
    Views:
    2,778
    Eric Sosman
    Jun 11, 2004
  5. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page