initialising a pointer with a function address

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
How to create a pointer to void function?

int sq(int num)
{
	return num*num;
}

int kub(int num)
{ 
	return num*num*num;
}

void central(int num, int(*p)(int))
{
	int result = (*p)(num);
	cout << result << endl;

}


int main()
{
	int num; cin >> num;
	int(*ptr)(int) = NULL;

	if(num<10)
	{
		ptr = sq;
	}

	else
	{
		ptr = kub;
	}

	void(*ptr)??????



	return 0;
}

[code]
[/code]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
int sq(const int& num)
{
	return num*num;
}

int kub(const int& num)
{
	return num*num*num;
}

int main()
{
	int num;
	std::cin >> num;

	int(*ptr1)(const int&) = sq;//function pointer declaration and assignment
    int(*ptr2)(const int&) = kub;


	(num < 10) ? std::cout<< ptr1(num) : std::cout << ptr2 (num);//passing function pointers

}
and what's with the void function?
What? You already seem to know how to create a function pointer (line 24) the only difference would be the return value and the parameters. void(*Vptr)() = nullptr;

But if what you're after is to pass the function pointer into the central() function then you would use something like the following in place of your "void" question.
central(num, ptr);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int num; cin >> num;
	int(*ptr)(int) = nullptr;
 
	if(num<10)
	{
		ptr = sq;
	}

	else
	{
		ptr = kub;
	}


        central(num, ptr);

	return 0;
}
I know that but how to make a pointer to a function that as an argument have a pointer to a function? How to make a pointer to void "centar(int broj, int(*p)(int))"?
void(*vPtr)(?????)
I know that but how to make a pointer to a function that as an argument have a pointer to a function

Declares a pointer-to-function named foo returning void accepting a pointer-to-function returning void and taking no parameters:
void (*foo)(void(*)()) = nullptr;
Declares a pointer to function named pcentral returning void accepting an int and a pointer to function returning int and accepting an int:
void (*pcentral)(int, int(*)(int)) = central;

It is usually a good idea to introduce an alias; the function pointer syntax is obscure enough as is:
Aliases a pointer-to-function returning int and accepting an int:
typedef int(*pfn)(int);
Declare central using that alias:
void central(int a, pfn b);
Declare a pointer to central:
void (*pcentral)(int, pfn b) = central;

P.S.:
Maybe you're using C, but if not, now is a good time to look into alternatives to function-pointers. You are likely to gain genericity and readability at the same time.
P.P.S.:
Sometimes http://cdecl.org/ is a useful aid (but it's not as good as a compiler).
Last edited on
Maybe something like this?
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
26
27
28
29
30
31
32
using fn1 = int(*)(int);
using fn2 = void(*)(int, fn1);

// ...

void central(int num, fn1 p)
{
    // ...
}

int main()
{
    fn1 ptr{};
    const fn2 ptr2{ central };
    
    int num; 
    std::cin >> num;


    if(num<10)
    {
        ptr = sq;
    }
    else
    {
        ptr = kub;
    }

    ptr2(num, ptr);
    
    return 0;
}


9
81

10
1000
Last edited on
Thx guys this helped a lot i get it now :D
Topic archived. No new replies allowed.