Re: array of pointers
On Apr 30, 12:22 am, Old Wolf <oldw...@inspire.net.nz> wrote:
On Apr 30, 4:10 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Jess wrote:
Then "x" is a const char*[3], and can decay into const char**. I've
heard about a pointer pointing to the entire array, but haven't seen
an example yet. For my example, how can I get a pointer pointing to
the whole "x" array?
const char* (*pa)[3] = &x;
In addition, what can we use it for?
Not sure. I can't recall ever needing one.
I use them in functions that expect to be passed a fixed-size array,
e.g.
cryptographic functions:
bool des_cbc_checksum( byte (*out)[8], void const *in, size_t
in_len );
In most such cases, I'd use a reference to the array, rather
than a pointer (unless, of course, I had to be compatible with
C).
If so, I guess it's the responsibility of this function to check (by
some method) if the argument is a pointer or not. Is this right?
No. The responsibility lies on the caller, in most cases. Pass the
size along and treat is an an array if the size > 0. Treat it as
a single object if the size == 0.
void foo(T* p, size_t s = 0);
Wouldn't it make more sense to use 1 as the size of a single object,
and have 0 be an error?
It depends. I can't think of a case where it would make sense
to have a parameter which can be either an array or a scalar,
but if it did, I'd probably use -1 as the flag for scalar, in
order to distinguish the case from an array with either 0 or 1
members.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34