Re: Problem with function pointers and structures

Discussion in 'C Programming' started by Jens Thoms Toerring, Jul 2, 2009.

  1. Simon Connah <> wrote:
    > This is most likely a stupid question but I am having difficulty
    > implementing it.


    > Basically I want to create a structure which holds data about an object
    > and then have a pointer to another structure which holds function
    > pointers to manipulate the data in the first structure (add, edit,
    > delete etc etc).


    > The problem is I am not entirely sure of the best way to go about this,
    > here is a basic mock up of what I have so far:


    > user.h


    > /* forward declaration */


    > struct user;


    > typedef struct user_interface
    > {
    > int (*add)(PGconn *, struct user *);
    > int (*delete)(PGconn *, struct user *);
    > int (*edit)(PGconn *, struct user *);
    > } user_interface;


    > typedef struct user
    > {
    > char *username;
    > char *password;


    > /* lots more... omitted for brevity */


    > struct user *interface;


    As far as I understood you this should be a pointer to the structure
    with the function pointers. Then you would need here

    user_interface *interface;

    > } user;


    The you can do in the C code

    user_interface if = { add, delete, edit );
    user a_user;

    a_user.interface = &if;

    a_user.interface->add( PGconn_arg1, &a_user );

    etc. Is it that what you're planning to do?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 2, 2009
    #1
    1. Advertising

  2. (Jens Thoms Toerring) writes:
    [...]
    > The you can do in the C code
    >
    > user_interface if = { add, delete, edit );
    > user a_user;
    >
    > a_user.interface = &if;
    >
    > a_user.interface->add( PGconn_arg1, &a_user );

    [...]

    You might want to choose an identifier other than "if".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 3, 2009
    #2
    1. Advertising

  3. Keith Thompson <> wrote:
    > (Jens Thoms Toerring) writes:
    > [...]
    > > The you can do in the C code
    > >
    > > user_interface if = { add, delete, edit );
    > > user a_user;
    > >
    > > a_user.interface = &if;
    > >
    > > a_user.interface->add( PGconn_arg1, &a_user );

    > [...]


    > You might want to choose an identifier other than "if".


    Ooops, yes, definitely;-)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 3, 2009
    #3
  4. Jens Thoms Toerring

    Moi Guest

    On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:

    > Keith Thompson <> wrote:
    >> (Jens Thoms Toerring) writes: [...]
    >> > The you can do in the C code


    >> > a_user.interface = &if;


    >
    >> You might want to choose an identifier other than "if".

    >
    > Ooops, yes, definitely;-)


    I would suggest "this" or "new"

    :)
    AvK
    Moi, Jul 3, 2009
    #4
  5. Moi <> writes:
    > On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
    >
    >> Keith Thompson <> wrote:
    >>> (Jens Thoms Toerring) writes: [...]
    >>> > The you can do in the C code

    >
    >>> > a_user.interface = &if;

    >
    >>
    >>> You might want to choose an identifier other than "if".

    >>
    >> Ooops, yes, definitely;-)

    >
    > I would suggest "this" or "new"
    >
    > :)


    Or "restrict" or "inline" if you're using a C90 compiler.

    :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 3, 2009
    #5
  6. Simon Connah <> wrote:
    > On 2009-07-02 21:13:20 +0100, (Jens Thoms Toerring) said:


    > > As far as I understood you this should be a pointer to the structure
    > > with the function pointers. Then you would need here
    > >
    > > user_interface *interface;
    > >
    > >> } user;

    > >
    > > The you can do in the C code
    > >
    > > user_interface ifc = { add, delete, edit );
    > > user a_user;
    > >
    > > a_user.interface = &ifc;
    > >
    > > a_user.interface->add( PGconn_arg1, &a_user );
    > >
    > > etc. Is it that what you're planning to do?


    > Yeah that is pretty much the plan.


    > I'm basically looking for a way to achieve some level of data
    > abstraction with my library so that client applications can perform
    > certain tasks on data without having to know specific implementation
    > details specifically the layout of the structures in use.


    > The real issue is to expose a public interface for manipulating the
    > structures which is sufficiently general to allow radical redesign of
    > the underlying implementation without requireing programs to be
    > rewritten to take advantage of new or improved code.


    To me it looks a bit as if you're still in a design stage of your
    project. There are still a lot of words like "some level", "certain
    tasks", "sufficiently general" etc. in your problem description
    that make it rather difficult to decide on a certain way of how to
    implement things.

    > While it would most likely be easier in an object orientated language,
    > it is more fun in C.


    It looks as if you intent to use some OO techniques implemented
    in C. That definitely could be a way to go. But if it is I can't
    deduced from what you have posted. All what's rather clear is
    that you seem to have some "users" (perhaps going to be stored in
    a database if I interpret the 'PGconn' variable correctly - it
    looks a bit like you intent to use PostgreSQL and, if that's the
    case, you might want to think about the question if there isn't
    a way to write your program to not bind on a certain database)
    to be added, changed or deleted. But that's about all what one
    (or at least I) can deduce from what you've posted. But I guess
    what the "client application' is going to be that has to do
    "certain tasks" remains a mystery for everyone but you, so
    commenting on that (and especially on how exactly to do it)
    is a bit difficult.

    Perhaps it would be advantageous to concentrate on the design
    at the moment and not to worry too much about how to implement
    it in C (or another language) at this stage. You may end up not
    using C if it is to be too cumbersome for what you want to do,
    or arrive at questions that are easier to answer;-)

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 3, 2009
    #6
  7. Keith Thompson <> wrote:
    > Moi <> writes:
    > > On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
    > >
    > >> Keith Thompson <> wrote:
    > >>> (Jens Thoms Toerring) writes: [...]
    > >>> > The you can do in the C code

    > >
    > >>> > a_user.interface = &if;

    > >
    > >>> You might want to choose an identifier other than "if".
    > >>
    > >> Ooops, yes, definitely;-)

    > >
    > > I would suggest "this" or "new"
    > > :)


    > Or "restrict" or "inline" if you're using a C90 compiler.
    > :cool:}


    Now you made me spew my last beer over the keyboard - and who
    is going to buy me a new one? At least insert some spoiler with
    a warning, you bastards!
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 3, 2009
    #7
  8. Jens Thoms Toerring

    Richard Bos Guest

    Keith Thompson <> wrote:

    > Moi <> writes:
    > > On Thu, 02 Jul 2009 23:54:25 +0000, Jens Thoms Toerring wrote:
    > >> Keith Thompson <> wrote:
    > >>> (Jens Thoms Toerring) writes: [...]


    > >>> > a_user.interface = &if;

    > >
    > >>> You might want to choose an identifier other than "if".
    > >>
    > >> Ooops, yes, definitely;-)

    > >
    > > I would suggest "this" or "new"
    > >
    > > :)

    >
    > Or "restrict" or "inline" if you're using a C90 compiler.
    >
    > :cool:}


    That's not funny... before C99, I _did_ often use "inline" as an
    identifier. Grmbl.

    Richard
    Richard Bos, Jul 4, 2009
    #8
    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. tweak
    Replies:
    14
    Views:
    2,778
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  3. Eric Sosman

    Re: Problem with function pointers and structures

    Eric Sosman, Jul 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    277
    Eric Sosman
    Jul 4, 2009
  4. jameskuyper

    Re: Problem with function pointers and structures

    jameskuyper, Jul 8, 2009, in forum: C Programming
    Replies:
    2
    Views:
    296
    jameskuyper
    Jul 9, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page