Jump to content
  • Advertisement
Sign in to follow this  
waterwalk

Type cast in function pointer in C

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I met a problem when learning function pointer in C. I'm puzzled by the following code:
double incr(double d);
int test(int i, int (*p)(int i));

int main()
{
    int i = 2;
    i = test (i, (int (*p)(int))incr);
    printf ("%d\n", i);

    return 0;
}

double incr (double d)
{
    return ++d;
}

int test (int i, int (*p)(int i))
{
    return (*p)(i);
}


I run this code, and get a surprising result: -858993460 Why is the result not 3? BTW: I use VS2003.

Share this post


Link to post
Share on other sites
Advertisement
The types int and double have different sizes.
If you cast from type
double (*)(double)
to type
int (*)(int)
you actually try to call a function that expects 8 bytes on the stack (the double parameter of incr) with a value that is just 4 bytes in size (the integer i). That will not work.

Share this post


Link to post
Share on other sites
Remmeber casting pointers causes to compiler to reinterprete the data pointed at by the pointer not to change data itself. So for example

#include <iostream>
using namespace std;
int main(){
float f= 2.0f;
float *pf=&f;
int *pi=(int*)pf;
cout << (*pi) << endl; //(*pi) is not equal to 2, not even close.
cin.get();
}


And since doubles and ints probably have different sizes on your system breaks it even more.

You could use a helper function though, but there maybe a cleaner way of doing whatever your trying to do (this is more likely true in c++ than c though).

#include <cstdio>
#include <cstdlib>

double incr(double d);
int incrh(int d);
int test(int i, int (*p)(int));

int main(){
int i = 2;
i = test(i,incrh);
printf ("%d\n", i);
system("pause");

return 0;
}

double incr (double d){
return ++d;
}

int incrh (int i){
return (int)incr(i);
}

int test (int i, int (*p)(int)){
return (*p)(i);
}

Share this post


Link to post
Share on other sites
I think i understand a little. I have thought type casting a function pointer equals to type casting function parameters. As shown in my first post, i've thought the function test is the same as :


int test(int i)
{
return (int)incr((double)i);
}



Oh, what a big mistake!

Thanks for all your replies!

Share this post


Link to post
Share on other sites
When you do have a good reason to use function pointers, a typedef usually makes life much nicer:


typedef double (*SomeFunctionPtr)(double d); //< new line

double incr(double d);
int test(int i, SomeFunctionPtr p); //< changed this line

int main()
{
int i = 2;
i = test (i, incr);
printf ("%d\n", i);

return 0;
}

double incr (double d)
{
return ++d;
}

int test (int i, SomeFunctionPtr p) //< changed this line
{
return (*p)(i);
}




And not only that, but it means you always use the same type - you can't make your mistake of casting an int* to a double* unless you explicitly try to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!