Function pointer is use to call function by it's address Why program doesn't print statement ?
Code:#include <stdio.h> void colour () { printf("Green colours"); } int main () { void (*fun_ptr)(void) = &colour; return 0; }
Function pointer is use to call function by it's address Why program doesn't print statement ?
Code:#include <stdio.h> void colour () { printf("Green colours"); } int main () { void (*fun_ptr)(void) = &colour; return 0; }
It no more calls the function, than saying
int answer = 42;
causes something to be seen on screen.
You need to call your function via it's pointer if you want something to happen.
Code:void (*fun_ptr)(void) = &colour; fun_ptr(); // call it!
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
oh Thank you What's wrong with code
main.c:24:5: error: too few arguments to function ‘fun_ptr’Code:#include <stdio.h> void addition (int a, int b) { int c = a + b; printf ("c = %d /n", c); } int main () { addition (2, 5); void (*fun_ptr)(int) = &addition; fun_ptr(); return 0; }
fun_ptr();
^~~~~~~
The error message is telling you exactly what's wrong: too few arguments to function ‘fun_ptr’
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
But I am passing two actual argument in main function
gone through following links
Too few arguments to function (C language Error)
> But I am passing two actual argument in main function
Yeah, but only when you're calling addition directly.
> void (*fun_ptr)(int) = &addition;
This is just a bunch of lies - addition takes two int parameters.
> fun_ptr();
More lies - you don't even supply one parameter.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
So delete line 11 and uncomment line 14.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You can leave off the & when assigning the colour function to the pointer. In C, a function name is a pointer to the function's code.Code:void (*fun_ptr)(void) = &colour;