tags:

views:

334

answers:

3
+7  Q: 

int (*p) [4] ??

Hi ..

int (*p) [4] ;

Is "p" pointer to array of 4 integers ?? or what ??

and How can I call "new" for this pointer ??

+7  A: 

Is "p" pointer to array of 4 integers?

Correct!

How can I call "new" for this pointer?

For example, p = new int[7][4].

FredOverflow
Err.. was +1 until the edit. new int[7][4] gives you a single block 28 ints wide.
Billy ONeal
humm .. so that I think p is pointer to 4 pointers of integer each of them points to array of 4 integers , it's right ??
Farah_online
@Billy: There is nothing wrong with my example. The type of `new int[7][4]` *is* `int (*)[4]`. Maybe it's more obvious with a typedef? `typedef int row[4]; row *p = new row[7];` This is exactly equivalent to my code, just more readable. Would you argue that this is wrong as well?
FredOverflow
@Farah: No, it's not a pointer to 4 *pointers* to integer, it's actually a pointer to 4 integers. Remember that a pointer can point to a single element of an array (just as a `char*` can point to a character of a C string).
FredOverflow
@FredOverflow: I stand corrected. Isn't the typedef version the same as new int[4][7] though? Why does the order get switched around?
Billy ONeal
@Billy: Welcome to C declarator syntax hell ;-) The best reference I could find was 5.3.4 (§5 and §6).
FredOverflow
@FredOverflow : yes ... I mean that p is a pointer to array of 4 pointers to integer , unless how can we reserve array of two-dimensional by 'p' ? if 'p' is pointer to array of 4 integer it should allows to reserve just one-diension array of 4 integer ?? , it is ??
Farah_online
@Farah: How can we say `int* p = new int[42]` then? The pointer is always one dimension less then the array.
FredOverflow
+3  A: 

The online CDECL evaluator is a helpful resource for questions like this:


cdecl

C gibberish ↔ English

int (*p)[4];

    declare p as pointer to array 4 of int
Seth
+1 That's pretty cool!
FredOverflow
...except that `int (*p)[4];` doesn't _declare_ `p`, but __defines__ it. (At least it is in C++, which this question is about. As Johannes once pointed out, in some areas C and C++ differ in what's a definition and what's a declaration.)
sbi
@sbi: Okay, but every definition is also a declaration ;) "C declarator syntax" is a well-accepted term.
FredOverflow
@FredOverflow; In C and C++, every definition is also a statement. Still, people rarely ever confuse "definition" and "statement", but confuse "definition" and "declaration" all the time.
sbi
@sbi: I'm pretty sure the definition `int foo() { return 42; }` is *not* a statement ;)
FredOverflow
@FredOverflow: `<blush>` Scratch that. Then what was it? Isn't the definition of a local, automatic variable considered a statement? Anyway, my point is: People keep messing up declarations and definitions. (For example, more than half of the answers to http://stackoverflow.com/questions/1410563/ were either misleading or plain wrong, in comments people kept suggesting silly counter-examples (`int i;` not a definition), and I had to add a quote from the standard to settle the ever repeating discussions about what's a class definition vs. class declaration.) So I'd like to be precise with it.
sbi
@sbi: Yes a local automatic variable is a _statement_: a _declaration-statement_. A _function-body_ consists of a _compound-statement_ which is `{` _statement-seq[OPT]_ `}` . A local automatic variable declaration (and definition) is a _declaration-statement_ consisting of a _block-declaration_ consisting of a _simple-declaration_. Outside of a function body a _simple-declaration_ may appear in a context where it is not a _statement_.
Charles Bailey
+3  A: 

int (*p)[4] is, indeed, a pointer to an array of four ints.

You can dynamically allocat an object of type "pointer to array of four int" as follows.

int (**ptr)[4] = new (int (*)[4]);

Note, no space for any ints is allocated; only the pointer itself.

You can allocated an array of 4 ints as follows:

int *ptr = new int[4];

What you can't do (without explicit casting) is assign a pointer to a dynamically allocated array of 4 int to a pointer of type int (*)[4]. Whenever you allocate an array via new, even if you use a typedef, the type of the new expression is a pointer to the first element of the array; the size of the array is not retained in the type of the new expression.

This is because new[] expressions can allocate arrays where the size of the array is chosen at runtime so would not always be possible (or even desirable) to encode the array size into the type of the new expression.

As has been suggested, you can dynamically allocate an array of one array of 4 int. The size of the first array is lost from the type information and what you get is a pointer to the first element of the array (of size 1) of the arrays of four int.

int (*p)[4] = new int[1][4];

Even though it is an array of just 1 (arrays of 4 int), you still need to use delete[] to deallocate p.

delete[] p;
Charles Bailey
Of course one should never actually be a position to need to call `delete` manually.
GMan
@Charles Bailey : well , what about int (*p)[4] = new int[4][4] ... dow can I call delet on it ?? I try : for (int i=0;i<4;i++) delete[] p[i] ; delete[] p ; but it doesn't work :(
Farah_online
@Farah_online: It doesn't matter what size the array is, you always delete arrays with `delete[] p;` So `p = new int[1][4];` ... `delete[] p` and `p = new int [4][4];` ... `delete[] p`. Calling delete on an array member (`delete[] p[i]` or `delete p[i]`) is not valid.
Charles Bailey