Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 98,765 C++ Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,040 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert

Register to Make This Box Go Away!


Functions stored in structure

 
Reply to this topicStart new topic

Functions stored in structure, using function pointers

boschow
post 8 Oct, 2007 - 06:54 AM
Post #1


New D.I.C Head

*
Joined: 7 May, 2007
Posts: 41


My Contributions


Hi all,
it is possible to declere a function within a structure, then in the main function with the use of the pointers call this function and assign the specific parameters . To make my idea clearer check out the sample code.

CODE

struct {
       int AND_function;
       int math_funct;
       int comp_funct;
       int regul_funct;
}functions;

AND_function (parameter1, parameter2, . . .)
{
                     .
                     .
                     .
}

int main()
{
     struct functions *fun;
     function.AND_function(par.parameter1, par.parameter);
}

Thanks for your help,

Best regards,

BoSCHoW.
User is offlineProfile CardPM

Go to the top of the page


NickDMax
post 8 Oct, 2007 - 08:11 AM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,245



Thanked 9 times

Dream Kudos: 400
My Contributions


Yes... and no. First of all. What language are you using C or C++?

Basically the answer is "yes" for C++ and "well no, but if you really want to sure why not" for C.

C does not natively support functions in structures. However it does support pointers and function pointers, and so you can (if you really feel like it), but the syntax gets a little messy I think.

My computer is busted else I would post an example. But I am sure if you would like to get into function pointers in C someone here can help you figure out the syntax.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 9 Oct, 2007 - 01:35 PM
Post #3


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,302



Dream Kudos: 500
My Contributions


If this is being done in c, you can use function pointers like so

CODE

struct {
       int (*AND_function_ptr)(int, int);
       int (*math_funct_ptr)(float, char, float);
       int (*comp_funct_ptr)(double);
       int (*regul_funct_ptr)(char*, char*, int);
}functions;

int AND_function (int parameter1, int parameter2)
{
                     .
                     .
                     .
}

int math_funct (float parameter1, char parameter2, float parameter3)
{
                    .
                    .
                    .
}

int comp_funct (double parameter)
{
                    .
                    .
                    .
}

int regul_funct (char *parameter1, char *parameter2, int parameter3)
{
                    .
                    .
                    .
}


int main (int argc, char **argv)
{
     functions f;

     //Assign function pointers to functions
     f.AND_function_ptr = AND_function;
     f.math_funct_ptr = math_funct;
     f.comp_funct_ptr = comp_funct;
     f.regul_funct_ptr = regul_funct;

     //Calling functions addressed by pointers
     f.AND_function_ptr (10,15);

     return 0;
}



This post has been edited by Dark_Nexus: 10 Oct, 2007 - 11:55 AM
User is offlineProfile CardPM

Go to the top of the page

Kiriran
post 17 Oct, 2007 - 11:23 AM
Post #4


New D.I.C Head

*
Joined: 11 Apr, 2007
Posts: 40


My Contributions


I'm just guessing now, but if you try to make an "Object" in C using structs and function pointers, that doesn't really work since you don't have a reference to the "Object" it self. So instead of creating function pointers, just pass the struct into a normal function and let the function manipulate the struct.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 18 Oct, 2007 - 06:01 PM
Post #5


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,245



Thanked 9 times

Dream Kudos: 400
My Contributions


well I am not a compiler designer nor really studied advanced language structures etc, but I do know that the Comeau C++ compiler actually compiles C++ into C. The point being that there is nothing all that mysterious and amazing about Objects. They represent some pretty complicated uses of pointers but they are just an application of pointers and structures.
User is offlineProfile CardPM

Go to the top of the page

Kiriran
post 19 Oct, 2007 - 03:10 PM
Post #6


New D.I.C Head

*
Joined: 11 Apr, 2007
Posts: 40


My Contributions


QUOTE(NickDMax @ 18 Oct, 2007 - 06:01 PM) *

well I am not a compiler designer nor really studied advanced language structures etc, but I do know that the Comeau C++ compiler actually compiles C++ into C. The point being that there is nothing all that mysterious and amazing about Objects. They represent some pretty complicated uses of pointers but they are just an application of pointers and structures.

yeah that's true. It would be just plain ugly to code it in C. Why not just use C++? And I think the resulting C code isn't too complicated. I'm just studying compiler construction and if I had to make Objects (if I compile it down to C code) I'd just make a struct and replace the function calls from the object with a normal C function call
CODE
#include <stdio.h>

struct obj {
    int a;
};

int objFunc(struct obj* this, int b) {
    return this->a * b; // return a * b;
}

int main(int argc, char *argv[]) {
    struct obj a; // obj a = new obj();
    a.a = 10;
    int b = objFunc(&a, 5); // a.objFunc(5);
    printf("%d\n", b);
    return 0;
}


tadaaa and there we have the this keyword cool.gif

This post has been edited by Kiriran: 20 Oct, 2007 - 04:09 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 20 Oct, 2007 - 07:06 AM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,245



Thanked 9 times

Dream Kudos: 400
My Contributions


There are great reasons for using function pointers in structures. Mostly though this is to add "hooks" so that different functions can be hung on them. Kind like the idea of a virtual function.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 7/20/08 02:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->