Passing pointer to function

Is there a different rule when we are passing a pointer to a function we sometimes write ampersand with it but when we are passing it throw array we don't write ampersand with it are there anymore cases when we don't have to write ampersand when passing it to a function with pointer argumnent

Functions are implicitly convertible to pointers to themselves, just how arrays are implicitly convertible to pointers to their first elements.

So if you're calling a function that takes a pointer to function, and there is no overload that takes a reference to function, you can omit the ampersand in the function call:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void g() {}

void f1( void(*)() ) {} // takes pointer to function

void f2( void(*)() ) {} // takes pointer to functino
void f2( void(&)() ) {} // takes reference to function

int main()
{
    f1(&g); // OK: explicitly build a pointer to functino
    f1(g); // OK: use implicit conversion

    f2(&g); // OK: calls the first overload
    f2(g); // Error: can't decide between pointer and reference
}


similarly, if you're initializing a pointer, you can omit the ampersand:

1
2
3
4
5
6
7
8
9
10
void g() {}

int main()
{
    void (*p1)() = &g; // OK, explicit address-of
    void (*p2)() = g; // OK, implicit conversion

    void (&r1)() = &g; // Error: can't bind reference to a function to a pointer
    void (&r2)() = g; // OK, reference to function
}
void (*p1)() = &g;

are you passing a function to a function i haven't read about it i am sorry but i don't understand completely let me give you an example of what my problem is
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
//case 1
void cube(*nm)
{
*num=*nm**nm**nm;
}
//case 2
void f(int *num)
{
	for(int i=0;i<5;++i)
	{
		num[i]=0;
	}
}
int main()
{
int number=2;
cube(&number);
int nu[5];
 f(nu);
   for(int i=0;i<5;++i)
	{
		cout<<nu[i]<<endl;
	}
   getch();
}
(

in case 1 we did write ampersand and in case 2 we didn't write ampersand so the question is are there any more cases where we don't have to write ampersand
Oh, I did misunderstand your question, I though you said "passing a pointer to a function" meaning the type "pointer to function", rather than the type "pointer" used as a function argument.

At least it should help you see how posting actual code is more helpful than describing it.

Could you be more specific about where do you expect to see an ampersand?
Last edited on
Yes :D ok so in case 1 ampersand was required for calling and in case 2 we didn't require ampersand for calling even we didn't write steric in the body in case 2 function so for an array we don't have to write ampersand even if it's of any kind of data-type?
Last edited on
in case 1 ampersand was required for calling and in case 2 we didn't require ampersand for calling


In both cases you have a function that expects a pointer to int, but the argument is an integer in the first case (int number;) and an array in the second case (int nu[5];).

If you use the address-of operator with the array, you will get a pointer to the array, not a pointer to the integer the function f expects. You simply can't compile f(&nu);

What you can compile is f(&nu[0]); which first selects the first int in the array and then uses & to build a pointer to it. This operation is so common that when you use the name of an array in a situation where a reference is not expected, it will be automatically replaced by &array[0]:

1
2
3
4
5
6
7
8
9
10
int main()
{
    int number; // int
    int* p1 = &number; // & produces pointer to int

    int nu[5]; // array of 5 int
    int (*p4)[5] = &nu; // & gives a pointer to array, not an int*
    int* p2 = &nu[0]; // this way you get a pointer to int
    int* p3 = nu; // exact same thing as = &nu[0];
}

Topic archived. No new replies allowed.