Home > Software design >  What is the difference between these two expressions
What is the difference between these two expressions

Time:11-15

#include <bits/stdc  .h>
using namespace std;
void test(){
    int a = 100;
    cout << a << endl;
}
int main()
{
    void(*b)() = test;
    (*b)();         //expression one
    b();            //expression two
    return 0;
}

test is a pointer to function, isn't it? (*b)() is a correct form, because it is equivalent to function prototype. But Why is it correct to delete a symbol *? They all produced the right results.

CodePudding user response:

test is a pointer to function, isn't it?

No, it isn't. test is a function.

b is a pointer to function.

But Why is it correct to delete a symbol *?

Because you can also invoke the function call operator on function pointers, and not just functions.

Furthermore, since a function can implicitly convert to a function pointer, this is also equivalent:

(********b)();

What is the difference between these two expressions

One indirects through function pointer and invokes function call operator - which calls the pointed function.

The other invokes function call operator on a function pointer - which implicitly indirects through the pointer and calls the pointed function.

The former requires 3 more characters to write.

CodePudding user response:

test is the function, b is a pointer to function (which points to test); and pointer to function could be used in function-call expression directly.

A pointer to function can be used as the left-hand operand of the function call operator, this invokes the pointed-to function:

Dereferencing a function pointer yields the lvalue identifying the pointed-to function:

int f();
int (*p)() = f;  // pointer p is pointing to f
int (&r)() = *p; // the lvalue that identifies f is bound to a reference
r();             // function f invoked through lvalue reference
(*p)();          // function f invoked through the function lvalue
p();             // function f invoked directly through the pointer

Dereference on b like *b you'll get the pointed function test. As the effect, (*b)(); and b(); do the same thing, i.e. invoke test.

CodePudding user response:

Well, to explain this properly, I need to start from the very basics.

Let's consider the following function:

void function(){
  // body
}

When the compiler compiles this function, function is a symbol representing the address where the function body is located in memory. So, the value of function is the memory address and can be seen as a pointer.

() is an operator that tells the computer to execute whatever is stored at the memory address that is referenced before.

Ignoring the type-checking, we could technically call the ()-operator on any pointer or address. However, to avoid potential bugs, we have type checking to prevent, for instance, operator-() being called on a data pointer.

So in practical terms, the following can be used:

function() 

auto f1 = function
f1()

void (*f2)() = f2
f2()

void (&f3)() = function
f3()
  •  Tags:  
  • c
  • Related