?
Solved

Is it possible to have a function inside a struct?

Posted on 2006-11-30
2
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

771 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