Solved

Is it possible to have a function inside a struct?

Posted on 2006-11-30
2
189 Views
Last Modified: 2010-04-15
Hi Experts,
Is it possible to have a function inside a struct, if so, can anyone tell me how?

-Rahul
0
Comment
Question by:rrahulgupta
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18048359
That's possible in C++, in C, you could only place a function pointer inside a struct, e.g.

int add(int a, int b) { return a + b;}

typdef int (*FuncPtr)(int,int);

struct foo {

int a;
int b;
FuncPtr ptr;
};

struct foo test;
int result;

test.a = 1;
test.b = 2;
test.ptr = add;

result = (*test.ptr)(test.a,test.b);

Clumsy, isn't it?
0
 

Author Comment

by:rrahulgupta
ID: 18048489
yeah, it is. :-)
anyway, thanks for the comment.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to align numbers in C using the %d 2 89
C#, VS15, StructLayout 1 122
Passing command line arguments in C 10 85
How to jump to matching brace in eclipse editor ? 1 264
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now