By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

struct and function pointer in C

P: n/a
Can a structure contain function pointers as members ?
If so, where is it useful ?

Feb 20 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
su**************@yahoo.com, 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.
Feb 20 '07 #2

P: n/a
ya we can do this .like

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

<su**************@yahoo.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Can a structure contain function pointers as members ?
If so, where is it useful ?

Feb 21 '07 #3

P: n/a
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;

<su**************@yahoo.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Can a structure contain function pointers as members ?
If so, where is it useful ?
Feb 22 '07 #4

P: n/a
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
Feb 22 '07 #5

P: n/a
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
Feb 23 '07 #6

P: n/a
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>
Feb 23 '07 #7

P: n/a
Flash Gordon <sp**@flash-gordon.me.ukwrites:
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) ks***@mib.org <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.
Feb 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.