0

I'm learning C. I know what the first line is doing; it's making a pointer to a function with no arguments and it returns an int. But wtf is the second doing?

My guess is that it is casting an int into a function? But what does it mean to turn an int into a function? Also, why does it cause an error when I try to call the function: 'function()'?

int (*function) ();
function = (int (*) ()) (1000);
1
  • Your understanding of the first line is incorrect. That line defines a variable which is a pointer to a function that takes an undetermined number of arguments. If you want a function that takes no arguments, you want int (*function)(void); Commented Oct 22, 2020 at 13:18

2 Answers 2

4

Overall, the code is nonsense. Where did you get it from?

it's making a pointer to a function with no arguments

Rather, it is making a pointer to a function with obsolete style parameter list.

But wtf is the second doing?

It assigns the function pointer to point at address 1000 (decimal), my means of a cast from int to the function pointer type.

why does it cause an error when I try to call the function: 'function()'?

Likely because there is no such function allocated at address 1000. You might not even have access to that area etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for answering my question so clearly! Usually, my question gets deleted by other people for various reasons.
0

learning function pointer is pain in ass. simple example maybe help for better understand.

suppose you want point to this function:

int sum (int a , int b) {
    return a+b;
}

so you need define a pointer with same type:

int *pointerToSum (int, int);

and simply assign it to function:

pointerToSum = ∑

if you want use it:

(*pointerToSum)(3, 5); //8

you can use function-pointer tag to find out more complex answers.

your question delete by people because there is duplicate question asked before.

I implement function-pointer for myself and it's a little complex and you can see in : exercise of function pointer

I wish you the best.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.