The Wayback Machine - https://web.archive.org/web/20240717024454/https://www.geeksforgeeks.org/difference-between-int-p-and-int-p/
Open In App

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

Last Updated : 11 Dec, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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


Similar Reads

Is there any difference between int[] a and int a[] in Java?
An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. In Java, Array can be declared in the following ways: One-Dimensional Arrays: The general form of a one-dimensional array declaration is type var-name[];ORtype[] var-name; Multidimensional Arrays: int[][] intArray
6 min read
Difference between int *a and int **a in C
In C, the declarations int *a and int **a represent two different concepts related to pointers. Pointers play a fundamental role in memory management and data manipulation in C programming so it is important to have a clear understanding of them. What does int * means? This declares a pointer to an integer. It means that the variable a can store th
3 min read
Difference between "int main()" and "int main(void)" in C/C++?
Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C/C++ Code int main() { /* */ return 0; } C/C++ Code int main() { /* */ return 0; } Definition 2: C/C++ Code int main(void) { /* */ return 0; } C
3 min read
Difference Between Unsigned Int and Signed Int in C
Integers are typically stored as 32-bit values, but in some environments, they may contain 16-bit values (or even a different number, usually the product of two powers). For example, let's examine 4-bit integers. They are small but can help illustrate a point. signed int can represent both positive and negative values, and unsigned int can only rep
2 min read
How to Use int as long long int for Competitive Programming?
Most of the time, input constraints in Competitive programming questions are bigger than the limits of int. Therefore, there is a need to use long int or even long long int. Here let us take two cases so that if a naive user writes correct logic still input is not getting accepted can get to know where the rectification is required. Case 1: Big int
3 min read
What’s difference between “array” and “&amp;array” for “int array[5]” ?
If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&amp;array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To check this, the very first thing that comes to min
4 min read
Maximum sum of a subsequence having difference between their indices equal to the difference between their values
Given an array A[] of size N, the task is to find the maximum sum of a subsequence such that for each pair present in the subsequence, the difference between their indices in the original array is equal to the difference between their values. Examples: Input: A[] = {10, 7, 1, 9, 10, 15}, N = 6Output: 26Explanation: Subsequence: {7, 9, 10}. Indices
5 min read
Assigning multiple characters in an int in C language
Consider the following C program. #include &lt;stdio.h&gt; int main(void) { int a = 'd'; printf(&quot;%d\n&quot;, a); /*OUTPUT - 100 (ASCII Code for character d)*/ int b = 'dd'; printf(&quot;%d&quot;, b); /*OUTPUT - 25700 (Explanation in detail given below)*/ return 0; } Output : 100 25700 We can easily guess that the output for 'd' is 100 as 100 i
1 min read
Java Math incrementExact(int x) method
The java.lang.Math.incrementExact() is a built-in math function in java which returns the argument incremented by one.It throws an exception if the result overflows an int.As incrementExact(int x) is static, so object creation is not required. Syntax : public static int incrementExact(int x) Parameter : x : the value to be incremented Return : This
1 min read
Convert String to int in C++
Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we can convert numbers that are stored as strings. Exa
8 min read
three90RightbarBannerImg