I totally understand what is "int *p[3]
" ( p is an array of 3 pointer meaning we can have 3 different rows of any number of ints by allocating the memory as our size of different rows).
My confusion lies with " int (*p)[3]
" what does this signifies? Is it like "p" stores the address of 3 contiguous memory of int or something else?
Please clarify and also how to use use in program to distinguish them.
Thanks a lot in advance.
@revised
Sorry for putting up duplicate question. I didn't search my doubt intensively. But my doubt still remains as novice programmer. I went through both the pages of Q/A C pointer to array/array of pointers disambiguation
and
int (*p) [4]?
second link partly clears the doubt so eliminate my doubt please explain above question in reference to stack and heap: for example
int *p[3]; // (1)
take 12(3*4bytes) bytes of stack and for heap will depend on run-time. Now for
int (*p1)[3]; //(2)
(2) using "new" would be one as
p1 = new int[7][3]; // (3)
given in one of the answer of link int (*p) [4]? ; Now my question is since " int (*p1)[3]; //(2) " is a pointer to am array of 3 ints so how much memory will be taken by p1 at compile time as eq(3) can a also be replaced by
p1 = new int[n][3]; // (3) where n is an integer
so what then?
Please explain.
They are the same. The first one considers p as a int * type, and the second one considers *p as an int . The second one tells you how C declarations simply work: declare as how would be used.
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++
The int *(a[3]) is the same as plain int *a[3] . The braces are redundant. It is an array of 3 pointers to int and you said you know what it means. The int (*a)[3] is a pointer to an array of 3 int (i.e. a pointer to int[3] type). The braces in this case are important.
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.
int *p[3]; // type of p is int *[3]
declares p
as an array 3 of int *
(i.e., an array of three int *
)
and
int (*p)[3]; // type of p is int (*)[3]
declares p
as a pointer to an array 3 of int
(i.e., a pointer to an array of three int
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With