C++ declarations (arrays and pointers)

J

joeschmoe98

i know that:

double *a[n]; is an array of n pointers

double (*b)[n]; is pointer to array of size n

but what is:

double ( *c[n] )( ); and

double ( *d( ) )[n];
 
S

Sam

joeschmoe98 said:
i know that:

double *a[n]; is an array of n pointers

double (*b)[n]; is pointer to array of size n

but what is:

double ( *c[n] )( ); and

double ( *d( ) )[n];

That looks like a question on your homework, which you should try to figure
out on your own.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHuNEkx9p3GYHlUOIRAsAsAJ9ijcP7HHrgahC5Oid7g45cd8goNgCfeRjs
h3x2UO4FNyCBftddxPeTttE=
=Ei5k
-----END PGP SIGNATURE-----
 
J

Jeff Schwab

joeschmoe98 said:
i just dont understand what the empty set of parenthesis are for

Please don't snip relevant context.

Empty parentheses in a declaration mean that the thing to their left is
a function.
 
J

Jeff Schwab

joeschmoe98 said:
still confused, at least point me in the right direction. please

When you post something new to a thread, please do not remove the
existing conversation. If re-post your question, please include your
best guess at what the confusing expressions mean.
 
J

James Kanze

i know that:
double *a[n]; is an array of n pointers
double (*b)[n]; is pointer to array of size n
but what is:
double ( *c[n] )( ); and
double ( *d( ) )[n];

The original rule, in C, was that the declaration mimics use.
This ceased to be true with the introduction of typedef, and of
course, const, volatile and class types positively wreck havoc
with the philosophy. But the fact remains that a C++
declaration reads a lot like an expression, with precedence,
which can be overridden by parentheses. In the case of such
"type expressions", precedence is simple: operators to the right
always have precedence over those to the left, and on any given
side, the operator closest to the center has precedence over the
one further out. The operators on the right may be "()"
(function) and "[]" (array). So you get:

double (*c[n])() ;

c[n] an array[n] of
*c[n] pointers to
(*c[n])() functions taking no arguments, returning
double (*c[n])() double

and

double (*d())[n] ;

d() a function taking no arguments, returning
*d() a pointer to
(*d())[n] an array[n] of
double (*d())[n] double

Note that it's generally best to avoid such complicated
declarations. They're even harder to get right when you write
then than they are to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top