Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Can we assign function return value to function pointer?

"One common mistake is to do this:"

Code
int foo()
{
}

int goo()
{
}

int main()
{
    int (*pFoo)() = foo; // pFoo points to function foo()
    pFoo = goo; // pFoo now points to function goo()
    return 0;
}

Code
1
pFoo = goo();

"This would actually assign the return value from a call to function goo() to pFoo, which isn’t what we want. We want pFoo to be assigned to function goo, not the return value from goo(). So no parenthesis are needed."

- Alex from learncpp.com

So I tried to do this and got compiler error and this completely makes sense because I am trying to assign int value to int function pointer.

But Alex did not talk about any compiler error so what exactly he was trying to explain here. Can someone please explain?
"It is a simple logic truth that, short of mass emigration into space, with rockets taking off at the rate of several million per second, uncontrolled birth rates are bound to lead to horribly increased death rates."

- Richard Dawkins (The Selfish Gene)
Can we assign function return value to function pointer?

Sure, if the function returns a pointer to a function.
Learning C++ or any other programming language requires thinking wide and keeping all the stuff learned into mind. It was so simple but I didn't think that. Thanks a lot for answer.
"It is a simple logic truth that, short of mass emigration into space, with rockets taking off at the rate of several million per second, uncontrolled birth rates are bound to lead to horribly increased death rates."

- Richard Dawkins (The Selfish Gene)
2 months passed and I am kind of lost in this.

Can you please tell me the syntax of making a function which returns a function pointer

I tried doing:

Code
(*int) a()
{
    return pointerFunction;
}

but it is not working.
"It is a simple logic truth that, short of mass emigration into space, with rockets taking off at the rate of several million per second, uncontrolled birth rates are bound to lead to horribly increased death rates."

- Richard Dawkins (The Selfish Gene)
Never done this before but after a bit of quick research I'll take a guess

fptr a()
{
     return &pointerFunction;
}
Here's a simple example of what your looking for.

Code
#include <stdio.h>

typedef void (*pfunc)(void);

void display_hello()
{
  fputs("Hello, World!\n", stdout);
}

pfunc try_this()
{
  return display_hello;
}

int main(int argc, char**argv)
{
  pfunc ans = try_this();
  fputs("Now call ans->", stdout);
  ans();
  return 0;
}


Or you could try something a little different...

Code
#include <stdio.h>

typedef int (*pfunc)(int);

int double_it(int x)
{
  return x * 2;
}

int sqr_it(int x)
{
  return x * x;
}

pfunc get_func()
{
  int choice = 0;
  fputs("Enter choice (1) double it (2) square it->", stdout);
  fscanf(stdin, "%d", &choice);

  if (choice < 2)
    return double_it;
  return sqr_it;
}

int main(int argc, char**argv)
{
  int val = 0;
  fputs("Enter a value to calculate->", stdout);
  fscanf(stdin, "%d", &val);

  fprintf(stdout, "ans->%d\n", (get_func())(val));
  return 0;
}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by SentienceOct 23, 2022 04:04 PM 10 294 Oct 23, 2022 04:04 PM
Last Post: Sentience
[C] by Equivalent_Jaguar198Jun 13, 2022 02:20 PM 4 188 Jun 13, 2022 02:20 PM
Last Post: Equivalent_Jaguar198
[Python] by MaskinMay 25, 2022 05:29 PM 4 374 May 25, 2022 05:29 PM
Last Post: Maskin