Computing.Net > Forums > Programming > function pointers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

function pointers

Reply to Message Icon

Name: w
Date: May 15, 2003 at 10:40:52 Pacific
OS: linux
CPU/Ram: ???
Comment:

======================================================================

hello,

i got a problem with c programming, could anybody be kind enough telling me what goes wrong?

i am writing a c program and using gcc in unix to compile it. well, i would like to use pointer to a function to replace if statements. I got them compiled perfectly, but when it is run, it gives an error :"Trace/Breakpoint Trap" and before an error:"Illegel Instruction".

well, basically the program is spilt into several files:
~~~~~~~~~~~~~~~~~~~~~
algo.h :
ThisStruct *best_fit(ThisStruct*, int);

algo.c:
define the above best_fit() function.

main.c :
ThisStruct* (*seek)(ThisStruct*, int); //declared in main function.

and used it in main function as well:
simulation(seek);

in sim.c
// define simulation function
void simulation(ThisStruct* (*seek)(FreeList*, int))

ThisStruct *ptr;
ThisStruct *node;
int anInteger;

...
...

ptr = seek(node, anInteger); // problem occurs
...
...

}

could anybody please tell me what the problem is? I did include all the header files properly, and the files got compiled perfectly (without any warning with gcc), but why when it is run, it gives the error memtioned above?

thanks for reading this message and thanks a lot for any suggestions and recommendation.

and well, please tell me as soon as possible if my question is not clear enough -i am very tired and sleepy now, but i need to get it done :(

thanks again.



Sponsored Link
Ads by Google

Response Number 1
Name: Sam (Java's Crypt)
Date: May 15, 2003 at 17:09:17 Pacific
Reply:

First of all, i hope you have linked all your odjects into a final executable.

I see that you declare seek as

ThisStruct* (*seek)(ThisStruct*, int);
This is a pointer to a functions that takes two arguments, (ThisStruct*,int) and returns a ThisStruct*.
------------------
You also declare simulation as

void simulation(ThisStruct* (*seek)(FreeList*, int))

This is a function that takes one argument and returns nothing.
The argument is a pointer to a function that takes two arguments(FreeList*,int) and returns ThisStruct*. You've named that argument "seek".
-----------------
So the first seek is a pointer to a function of type : ThisStruct*(ThisStruct*,int)
And the second seek is a pointer to a function of type: ThisStruct*(FreeList*,int)

I suppose ThisStruct and FreeList are not the same thing. In that case "node" is a ThisStruct* but it is passed to the "second seek".(The argument of the "simulation" function). This seek takes an argument FreeList* and not an ThisStruct*. So the problem is either in one of the following two lines
---------------
void simulation(ThisStruct* (*seek)(FreeList*, int))

ThisStruct *node;
---------------
Either the seek must take a ThisStruct* as an argument and not a FreeList*,
or
node must be a FreeList* and not a ThisStruct*.

I believe the problem is at the declaration of simulation. I think it should be like that

void simulation(ThisStruct* (*seek)(ThisStruct*, int)).

NOT void simulation(ThisStruct* (*seek)(FreeList*, int))
----------------

To avoid such probles try to define function pointer types using typedef.

If you want to define a pointer to a function that takes an int and a char as arguments and returns a float you can do that this way:

typedef float(*ICtoF)(int,char);

Now you have defined the type ICtoF.
Instances of that type are pointers to functions like the above.

Eg code

typedef float(*ICtoF)(int,char);

float myfunc(int a,char c) // our function
{
float b = (float)(a + (int)c) + 0.5;
return b;
};

void main()
{
float a;
int b = 7;
char c = 'k';
ICtoF func; // Func is a pointer to a function like myfunc
func = myfunc;
a = func(b,c);
};

S.A.Y.S. (Sam At Your Service) ;) :)


0
Reply to Message Icon

Related Posts

See More


C++ Homework Help ****Detecting DTMF using ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: function pointers

Function pointer www.computing.net/answers/programming/function-pointer/5002.html

c++ function pointers www.computing.net/answers/programming/c-function-pointers/9516.html

pointers to function www.computing.net/answers/programming/pointers-to-function/4973.html