Solved

Is it possible to have a function inside a struct?

Posted on 2006-11-30
2
190 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

810 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