Difference between int (*p)[3] and int* p[3]?
Last Updated :
24 Nov, 2020
Pointers store the address of variables or a memory location. Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format:
Syntax:
datatype *var_name;
Example:
int *ptr;
In this example “ptr” is a variable name of the pointer that holds address of an integer variable.
In this article, the focus is to differentiate between the two declarations of pointers i.e., int (*p)[3] and int *p[3].
For int (*p)[3]: Here “p” is the variable name of the pointer which can point to an array of three integers.
Below is an example to illustrate the use of int (*p)[3]:
C++
#include <iostream>
using namespace std;
int main()
{
int (*p)[3];
int a[3] = { 1, 2, 3 };
p = &a;
for ( int i = 0; i < 3; i++) {
cout << *(*(p) + i) << " " ;
}
return 0;
}
|
For int *p[3]: Here “p” is an array of the size 3 which can store integer pointers.
Below is an example to illustrate the use of int *p[3]:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * p[3];
int a = 1, b = 2, c = 3;
p[0] = &a;
p[1] = &b;
p[2] = &c;
for ( int i = 0; i < 3; i++) {
cout << *p[i] << " " ;
}
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...