Skip to content
Related Articles

Related Articles

Difference between int* p() and int (*p)()?
  • Difficulty Level : Expert
  • 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

14

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :