Difference between int* p() and int (*p)()?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is:
Syntax:
type *var_name;
Here, type is the pointers base type. It must be a valid C/C++ data type and var-name is the name of the pointer variable. The asterisk * is being used to designate a variable as a pointer. Following are the valid pointer declaration for their respective data type:
int *ip; float *fp; double *dp; char *cp;
In this article, the focus is to differentiate int* p() and int (*p)().
int* p(): Here “p” is a function that has no arguments and returns an integer pointer.
int* p() returntype function_name (arguments)
Below is the program to illustrate the use of int* p():
C++
// C++ program to demonstrate the use// of int* p()#include <bits/stdc++.h>using namespace std; // Function that returns integer pointer// and no argumentsint* p(){ int a = 6, b = 3; int c = a + b; int* t = &c; return t;} // Driver Codeint main(){ // Declare pointer a int* a = p(); cout << *a;} |
9
int (*p)(): Here “p” is a function pointer which can store the address of a function taking no arguments and returning an integer. *p is the function and ‘p‘ is a pointer.
Below is the program to illustrate the use of int (*p)():
C++
// C++ program to demonstrate the use// of int* (*p)()#include <bits/stdc++.h>using namespace std; // Function with no arguments// and return integerint gfg(){ int a = 5, b = 9; return a + b;} // Driver Codeint main(){ // Declaring Function Pointer int (*p)(); // Storing the address of // function gfg in function // pointer p = gfg; // Invoking function using // function pointer cout << p() << endl;} |
14






Please Login to comment...