Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert int* to int (*p)(void)?

Tags:

c

pointers

I'm trying to do it this way:

int (*p)(void);
int *i;
...
p = (int *(void))i;

But it's causing syntax error:

error: cast specifies function type

What's wrong here?

like image 868
Je Rog Avatar asked Dec 21 '22 11:12

Je Rog


2 Answers

You should respect error in this case. You must not convert a pointer to function to an int pointer or other way around. That might result in undefined behavior.

If you insist then, syntax should be:

p = (int (*)(void))i;
like image 200
iammilind Avatar answered Dec 24 '22 00:12

iammilind


i is a pointer to an integer, and p is a function pointer returning and int and taking no arguments. They are not compatible/castable in any way, shape or form.

like image 23
Tony The Lion Avatar answered Dec 24 '22 00:12

Tony The Lion