Re: new an array of pointers
On Mar 19, 1:13 am, thomas <freshtho...@gmail.com> wrote:
I ask this question because I thought "int *" is just like a type like
"int" and
It is. But note that there are two syntaxes for new
expressions:
new new-type-id
and
new ( type-id )
(both simplified by eliminating the optional bits). And a
new-type-id only accepts a small subset of legal type
expressions; for anything more general, you need the second
form.
------
int **p = new int* [30];
-----
just like
----
int *p = new int [30];
----
Those two are fine. Basically, a new-type-id is one or more
type-specifiers (things like int, const or names of classes),
followed by zero or more ptr-operator (a *), followed by zero or
more direct-new-declaration ([x], with the requirement that in
all but the first, the x must be a constant). For anything
else, you need the second form, with parentheses.
I thought it is obvious and natural until problem occurs.
I tried the expression
-----
int main(){
int **p = new (int (*([30])));}
-----
and got error message again in VC2005
----
1>------ Build started: Project: test, Configuration: Debug Win32
------
1>Compiling...
1>test.cpp
1>e:\lab\test\test\test.cpp(3) : error C3409: empty attribute block is
not allowed
1>e:\lab\test\test\test.cpp(3) : error C2143: syntax error : missing
']' before 'constant'
1>e:\lab\test\test\test.cpp(3) : error C2059: syntax error :
'constant'
1>e:\lab\test\test\test.cpp(3) : error C2059: syntax error : ')'
1>Build log was saved at "file://e:\Lab\test\test\Debug\BuildLog.htm"
1>test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
----
So I think more clarification may be required.
Compiler error? At first view, your code looks correct, and
both g++ and Sun CC accept it.
--
James Kanze