Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question in structures (C langauge) ?

Status
Not open for further replies.

SphinX

Advanced Member level 3
Joined
Jan 25, 2002
Messages
822
Helped
58
Reputation
116
Reaction score
29
Trophy points
1,308
Location
EGYPT
Activity points
7,045
Salam,

While i am reading about Text Interfaces i faces this structure

Code:
typedef  struct    _selection
{
       char   prompt[MAX_PROMPT_LEN];
       int      (*function)(int);
       int      fn_arg;
} SELECTION

What should this line do ?

Code:
int      (*function)(int);

Thanks
 

Thats a pointer to a function that takes an int as argument.

I would suppose it points to the routine that should handle that specific selection entry.

/Bingo
 

    SphinX

    Points: 2
    Helpful Answer Positive Rating
having pointers to functions is very useful in OS ..where you can run code dynamicaly youd create an array of pointer of functions and call them all with a loop ..Elegant and very small code
 

    SphinX

    Points: 2
    Helpful Answer Positive Rating
you can do many function for that function pointer for many options.
Just like C++ function overloading.
 

    SphinX

    Points: 2
    Helpful Answer Positive Rating
Is a pointer to a function ... look this example:

Code:
//Your typedef struct
typedef  struct    _selection 
{ 
       char   prompt[MAX_PROMPT_LEN]; 
       int      (*function)(int); 
       int      fn_arg; 
} SELECTION;

SELECTION mySel;


//various func with same return value and passing parameters 
// like "pointer to a function"

int foo1(int a)
{
    ...
    ...
}

int foo2(int a)
{
    ...
    ...
}

int foo3(int a)
{
    ...
    ...
}


// two functions that make the same thing

void Func(int val)
{

    for(int i=0; i<1000; i++) {
        switch(val) {
            case 1:
                foo1(val);
                break;
            case 2:
                foo2(val);
                break;
            case 3:
                foo3(val);
                break;
         }
    }
}

void BetterFunc(int val)
{
    switch(val) {
        case 1:
            mySel.function = foo1;
            break;
        case 2:
            mySel.function = foo2;
            break;
        case 3:
            mySel.function = foo3;
            break;
     }
     
    for(int i=0; i<1000; i++) 
        mySel.function(val);
}

"Func" e "BetterFunc" functions make the same thing.
In a "Func" function I repeat for 1000 times an "conditional jump" (switch), and then I call my appropriate function (foo1, foo2, foo3) ...
In a "BetterFunc" function I use the "pointer to a function" ... thus I can transport the "conditional jump" (switch) outside the loop, executing it only one time ... and I repeat for 1000 times ONLY the call to my appropriate function (foo1, foo2, foo3) via "pointer to a funcion" (mySel.function)

Mik
 

    SphinX

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top