Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between int (*p)[3] and int *p[3]? [duplicate]

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.

like image 450
zeal Avatar asked Aug 12 '13 18:08

zeal


People also ask

What is the difference between int * p and int p * and int * p?

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.

What does the following C declaration defines int (* p 3 ]) int int );?

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

What is the meaning of int (* a 3?

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.

What is the significance of int (* p?

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.


1 Answers

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)

like image 119
ouah Avatar answered Sep 21 '22 00:09

ouah