Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between int* p() and int (*p)()?

  • Difficulty Level : Medium
  • 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:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
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 arguments
int* p()
{
    int a = 6, b = 3;
    int c = a + b;
    int* t = &c;
    return t;
}
  
// Driver Code
int main()
{
    // Declare pointer a
    int* a = p();
    cout << *a;
}
Output:
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 integer
int gfg()
{
    int a = 5, b = 9;
    return a + b;
}
  
// Driver Code
int 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;
}
Output:
14



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!