Difference between int* p() and int (*p)()?
Last Updated :
11 Dec, 2020
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++
#include <bits/stdc++.h>
using namespace std;
int * p()
{
int a = 6, b = 3;
int c = a + b;
int * t = &c;
return t;
}
int main()
{
int * a = p();
cout << *a;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int gfg()
{
int a = 5, b = 9;
return a + b;
}
int main()
{
int (*p)();
p = gfg;
cout << p() << endl;
}
|
Share your thoughts in the comments
Please Login to comment...