struct and function pointer in C

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Feb 20, 2007.

  1. , India

    , India Guest

    Can a structure contain function pointers as members ?
    If so, where is it useful ?
     
    , India, Feb 20, 2007
    #1
    1. Advertising

  2. , India said:

    > Can a structure contain function pointers as members ?


    Yes.

    > If so, where is it useful ?


    It's useful in cases where you need to associate a function pointer with
    other data.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Feb 20, 2007
    #2
    1. Advertising

  3. , India

    newsGroups Guest

    ya we can do this .like

    struct abc
    {
    int a;
    char name[10];
    int *(functionptr)(int,int);
    }n,m;



    <> wrote in message
    news:...
    > Can a structure contain function pointers as members ?
    > If so, where is it useful ?
    >
     
    newsGroups, Feb 20, 2007
    #3
  4. don't top post


    newsGroups wrote:
    > ya we can do this .like
    >
    > struct abc
    > {
    > int a;
    > char name[10];
    > int *(functionptr)(int,int);


    int (*functionptr)(int,int);


    > }n,m;
    >
    >
    >
    > <> wrote in message
    > news:...
    > > Can a structure contain function pointers as members ?
    > > If so, where is it useful ?
    > >
     
    Nick Keighley, Feb 22, 2007
    #4
  5. On 20 Feb 2007 04:04:16 -0800, "subramanian100in@...com, India" wrote:
    >Can a structure contain function pointers as members ?
    >If so, where is it useful ?


    The following article may give some ideas. It is not perfect and may
    be improved but it shows one way to program polymorphically in C:
    http://www.codeproject.com/cpp/PolymorphismC.asp

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Feb 22, 2007
    #5
  6. , India

    Flash Gordon Guest

    Roland Pibinger wrote, On 22/02/07 20:18:
    > On 20 Feb 2007 04:04:16 -0800, "subramanian100in@...com, India" wrote:
    >> Can a structure contain function pointers as members ?
    >> If so, where is it useful ?

    >
    > The following article may give some ideas. It is not perfect and may
    > be improved but it shows one way to program polymorphically in C:
    > http://www.codeproject.com/cpp/PolymorphismC.asp


    It certainly needs some work. It uses a macro name which is an
    underscore followed by a capital in the header, and such names are
    reserved for the implementation. It uses the form
    type *ptr = (type *)malloc(sizeof(type));
    instead of the simpler and more robust
    type *ptr = (type *)malloc(sizeof(type));


    Also not specifying in prototypes when functions do not take parameters.

    I've not actually downloaded the code to check the rest of it.
    --
    Flash Gordon
     
    Flash Gordon, Feb 23, 2007
    #6
  7. , India

    CBFalconer Guest

    Flash Gordon wrote:
    >

    .... snip ...
    >
    > It certainly needs some work. It uses a macro name which is an
    > underscore followed by a capital in the header, and such names
    > are reserved for the implementation. It uses the form
    >
    > type *ptr = (type *)malloc(sizeof(type));
    > instead of the simpler and more robust
    > type *ptr = (type *)malloc(sizeof(type));


    I think you failed to complete the editing :) I fail to see any
    gains in simplicity or robustness. Here's something to cut and
    paste:

    type *ptr = malloc(sizeof *ptr);

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Feb 23, 2007
    #7
  8. Flash Gordon <> writes:
    > Roland Pibinger wrote, On 22/02/07 20:18:
    >> On 20 Feb 2007 04:04:16 -0800, "subramanian100in@...com, India" wrote:
    >>> Can a structure contain function pointers as members ?
    >>> If so, where is it useful ?

    >> The following article may give some ideas. It is not perfect and may
    >> be improved but it shows one way to program polymorphically in C:
    >> http://www.codeproject.com/cpp/PolymorphismC.asp

    >
    > It certainly needs some work. It uses a macro name which is an
    > underscore followed by a capital in the header, and such names are
    > reserved for the implementation. It uses the form
    > type *ptr = (type *)malloc(sizeof(type));
    > instead of the simpler and more robust
    > type *ptr = (type *)malloc(sizeof(type));


    Simpler, more robust, and identical.

    I presume you meant:

    type *ptr = malloc(sizeof *ptr);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 23, 2007
    #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. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  2. Ole
    Replies:
    4
    Views:
    602
    Michael Wojcik
    Oct 26, 2004
  3. beetle
    Replies:
    2
    Views:
    912
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    658
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    471
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page