Function pointers

Hi all,

There are two functions where in I have use function Validate as a pointer to the PrintNum function.

How to use the function pointers.

1
2
3
4
5
6
7
8
9
10
11
  int PrintNum(int z)
  {
   std::cout<<z<<std::endl;
  }
  int Validate(int x ,int y)
  {
    if(x<5)
    return x;
    else
    return y;
   }


int(*Validate)(int,int){&PrintNum} // Is this the way to use pointers
Validate(5,6 );

Thank you.
Here's a demo how to use PrintNum with a function ptr.
Try to do it for Validate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <iostream>

int PrintNum(int z)
{
  std::cout << z << std::endl;
  return z;
}
int Validate(int x, int y)
{
  if (x < 5)
    return x;
  else
    return y;
}

int main()
{
  // funtion ptr to call PrintNum
  int (*print_num)(int) = PrintNum;

  // call PrintNum through print_num and out the result
  std::cout << print_num(5) << '\n';

}
Topic archived. No new replies allowed.