6

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.

Community
  • 1
  • 1
zeal
  • 465
  • 2
  • 11
  • 22

2 Answers2

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

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 5
    I think the wording would be less awkward if instead of "array N of T" it read "array of N T". – cdhowie Aug 12 '13 at 18:27
  • 1
    Is there a different syntax that could be used for the second declaration? Something that would make it a little bit more obvious what is being declared. – Ben C. Aug 12 '13 at 18:32
  • 1
    @BenC. If you know C, then it's pretty obvious. –  Aug 12 '13 at 18:36
  • 2
    @BenC.: `typedef int ArrayOf3Int[3]; ArrayOf3Int *p;`. – Eric Postpischil Aug 12 '13 at 18:37
  • @H2CO3, DyP, Borgleader, KingsIndian, Eric Postpischil guys please look into my revised doubt and i earnestly request to spare some time to clear it. My doubt lies in "how much memory of stack will be eaten by the statement 'int (*p)[3]", please answer this with some explanation. I would be really grateful for kind gesture. – zeal Aug 13 '13 at 19:21
3

The syntax for declarations is a bit cumbersome in C, and the same syntax was inherited by C++. In particular arrays and functions wrap the declaration. If you keep this in mind it might help you:

int (*p)[3];
     *p      // p is a pointer to 
int (  )[3]  // an array of 3 int

You can consider breaking the declaration into two lines:

typedef int int3[3];
int3 *p;
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Rodríguez I have got earnest request for you. If you have read all the comment then i suppose you already know the question I'm asking to you. My question is since the statement [int *p[3]; will eat 12 byte of memory on stack and so p[0] will point to an array of memory if allocated dynamically or to some memory on stack(or an array) so does p[1], p[2]. But what about [int (*p1)[3]; how many memory of stack it will take. How to use malloc for this as for earlier one we can use p[0] = malloc( n0 * sizeof(int)); similarly for p[1], p[2], but what about int (*p1)[3]; how to use in. – zeal Aug 16 '13 at 18:35
  • @zeal: I think for this it is better if we consider a 64bit architecture for which pointers are 8bytes, but `int` remains 4 bytes (which is actually the common case). `int *p[3];` is an array of 3 pointers. If allocated in the stack it will require `8*3=24` bytes. Each one of the pointers will be able to refer to an `int` (or multiple contiguous `int`s) stored elsewhere. `int (*p)[3]` on the other hand is a pointer to an array of 3 `int`. It will take 8 bytes locally to hold the single pointer, and if initialized appropriately it will point to a block of `4*3=12` bytes elsewhere. – David Rodríguez - dribeas Aug 16 '13 at 18:45
  • Rodríguez, ok taking the case of 64bit architecture [int *p[3];will take 24 bytes of memory on stack whether we do dynamic allocation or not but what about int (*p1)[3]; how much it will take on stack and please be specific what do you mean by elsewhere(i this u r referring to heap only). And please show with one short example how to use malloc for this case. Thanks for earlier answer. – zeal Aug 17 '13 at 06:36
  • @zeal: I was (I believe) quite specific on the amount that the object will need locally. The *elsewhere* is because a pointer can refer to memory that is static, stack allocated or in the heap. If you `new`, then it will be dynamically allocated, if you create the appropriate data structure in the stack or statically allocated, then it will be in that location. It is *elsewhere* from the point of view of the pointer. – David Rodríguez - dribeas Aug 18 '13 at 02:24