By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,704 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,704 IT Pros & Developers. It's quick & easy.

Complex array definition

P: n/a
Recently, the following definition was posted to comp.lang.c++:
char **Arr[3][2];


1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?

2) Could the object be passed to functions with the given prototypes?

void foo( char **bar[][2] ); /* yes? */
void baz( char ****bar ); /* no? */
void quux( char **(*)bar[2] ); /* yes...? */

I don't know if the last is even legal; hence the question.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
void quux( char **(*)bar[2] ); /* yes...? */ I don't know if the last is even legal; hence the question.


Perhaps I intended to write

void quux( char **(bar*)[2] );

although the question still stands.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2

P: n/a
i think char **bar[2][3] is a two dimentional array of double pointers
(pointer to a pointer that points to a char). i believe it was just a
mistake, he really wanted to create a 2-d array of chars, and only needed
to delcar:
char **twodarray;
as for weather or not those are legal ways of passing the **char[][] to a
function, i don't know.

Nov 14 '05 #3

P: n/a
In article <cm**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Recently, the following definition was posted to comp.lang.c++:
char **Arr[3][2];
1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?


Yes.
Quoth cdecl:
cdecl> explain char **arr[3][2]
declare arr as array 3 of array 2 of pointer to pointer to char

2) Could the object be passed to functions with the given prototypes?

void foo( char **bar[][2] ); /* yes? */
Correct. The empty [] in the function declaration are just syntactic
sugar for a pointer, and the type described is the same type as the array
name decays to (array of foo -> pointer to foo), "pointer to array[2]
of pointer to pointer to char".
Quoth cdecl:
cdecl> explain char **bar[][2]
declare bar as array of array 2 of pointer to pointer to char

void baz( char ****bar ); /* no? */
Correct. "pointer to array" does not decay to "pointer to pointer",
so the conversion here is illegal.

void quux( char **(*)bar[2] ); /* yes...? */

I don't know if the last is even legal; hence the question.


'Tisn't, and neither is your correction in your other post, but I suspect
you may have meant:
void quux( char **(*bar)[2] );
which describes the same type that foo() takes (and that the array name
decays to), without using empty-[]-in-prototype-means-pointer.
Quoth cdecl:
cdecl> explain char **(*bar)[2]
declare bar as pointer to array 2 of pointer to pointer to char
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
[Y]our left hand is currently stopping string 2 fret 1, which is the note
C, so I guess this thread is about as topical as several other comp.lang.c
threads at present. --Richard Heathfield in comp.lang.c
Nov 14 '05 #4

P: n/a
Dave Vandervies <dj******@csclub.uwaterloo.ca> spoke thus:
Quoth cdecl:


I suspect I should have been aware of cdecl before, but at least I got
a chance to show some level of intelligence without using it. Thanks
for your answers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5

P: n/a
Dave Vandervies wrote:
In article <cm**********@chessie.cirr.com>,
Quoth cdecl:
cdecl> explain char **arr[3][2]
declare arr as array 3 of array 2 of pointer to pointer to char


<snip>

Hi,
I am a newbie C learner. Could you tell me what 'cdecl' is and how to
use it? Everyone in this group seems to know it except me.
~Jane

Nov 14 '05 #6

P: n/a
In article <10*********************@c13g2000cwb.googlegroups. com>,
Jane Andrews <ja**********@gmail.com> wrote:
Dave Vandervies wrote:
Quoth cdecl:
cdecl> explain char **arr[3][2]
declare arr as array 3 of array 2 of pointer to pointer to char
<snip>

Hi,
I am a newbie C learner. Could you tell me what 'cdecl' is and how to
use it? Everyone in this group seems to know it except me.


It's a program that understands enough C, and a large enough (though
highly structured and limited) subset of English, to translate between C
declarations (hence the name) and something slightly more human-readable
and -writeable.

It does two things that are useful for making sure you're right before
claiming something in comp.lang.c and for determining (by the fact that
you need them) that it's time to throw some typedefs at your code to
make it clearer:

explain <C>
will attempt to parse the C declaration you give it and give you an
English description of the type being declared (or, if it can't parse it,
gives the Very Helpful message "syntax error").
F'rexample:
--------
cdecl> explain int *p
declare p as pointer to int
--------

declare <name> as <English>
will attempt to identify the type you described and output a C declaration
of that type. F'rexample:
--------
cdecl> declare p as pointer to int
int *p
--------
It's actually only really a useful tool for language-lawyering or for
decoding code old enough that you can't hunt down and kill the programmer
who originally wrote it; if you think you need it for writing new code,
what you really need is to clean up and/or give better documentation
for the declarations.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.caI am trying to move 3 bits from left to right with >> operator

Try holding the 3 bits in place, but shift the rest of your computer's memory
right to left. --Kaz Kylheku gives stupid answers to stupid questions in CLC
Nov 14 '05 #7

P: n/a
That was very helpful. Thank You. I have just one more query. Where do
I get this program? :D
-Jane

Nov 14 '05 #8

P: n/a
In article <10**********************@z14g2000cwz.googlegroups .com> "Jane Andrews" <ja**********@gmail.com> writes:
That was very helpful. Thank You. I have just one more query. Where do
I get this program? :D


With some OS's it comes along. Otherwise:
<http://sources.isc.org/devel/tools/cdecl.txt> for instance.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #9

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<cm**********@chessie.cirr.com>...
Recently, the following definition was posted to comp.lang.c++:
char **Arr[3][2];
1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?


Here's how to deconstruct it.

Arr -- Arr
Arr[3] -- is a 3-element array
Arr[3][2] -- of 2-element arrays
*Arr[3][2] -- of pointers
**Arr[3][2] -- to pointers
char **Arr[3][2] -- to char

So yes, you got it right.

Whenever you come across a hairy declarator like that, start with the
identifier and work your way out according to operator precedence
(subscripts have higher precedence than indirection, so you know that
Arr is an array of pointers, not a pointer to an array).
2) Could the object be passed to functions with the given prototypes?

void foo( char **bar[][2] ); /* yes? */
Yes.
void baz( char ****bar ); /* no? */
No.
void quux( char **(*)bar[2] ); /* yes...? */

I don't know if the last is even legal; hence the question.


No. Here's how to construct it (again, start with the identifier and
work your way outward according to operator precedence):

bar -- bar
*bar -- is a pointer
(*bar)[2] -- to a 2-element array
*(*bar)[2] -- of pointers
**(*bar)[2] -- to pointers
char **(*bar)[2] -- to char
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.