Solved

Is it possible to have a function inside a struct?

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

Industry Leaders: 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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

738 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