473,414 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

struct and function pointer in C

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

Feb 20 '07 #1
7 1794
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Yoon Soo | last post by:
Hi, there while mixing assembler and c code I got the following question: struct bar { int bar_val1; int bar_val2; (...) }
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
6
by: Fernan Bolando | last post by:
What is the best way of passing a structure to a function currently I do by it passing a pointer to the structe. sample code below int main() { struct sample_struct a_struct, *point_struct;...
4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.