Casting for a fish that never arrived

John Woods john at x.UUCP
Thu May 16 05:13:50 AEST 1985


> /*-----------------------------------------------------------------------*/
> 
> char *Cwords[2][4] = {
> 	{"hello","go","here","see"},
> 	{"goodbye","come","there","hear"} 
> };
> char *Pwords[2][4] = {
> 	{"foo","foo","foo","foo"},
> 	{"foobar","foobar","foobar","foobar"} 
> };
> char *Words;
> 
	Words = Cwords;
	dot dot dot
> 			printf("%s\n",(CASTING EXPRESSION)Words[i][j]);
> 
> Here is a solution which works.  I find it hard to believe this
> can't be done without a typedef (or explicit pointer arithmetic), but
> haven't been able to find a way yet.
> 
> typedef char *SYNTAX[4];
> SYNTAX Cwords[2] = {
> SYNTAX Pwords[2] = {
> SYNTAX *Words;

Well, I personally prefer the typedef style, but you didn't have to use
it.  The basic problem in the first example is that Words should not be
of type char *;  if Words were declared as a pointer to an array of four
char pointers, e.g.

char *(*Words)[4];

then you could use Words[i][k] without any casting required.
I *THINK* I have the parentheses in the right place, and some quick cc -S
experiments seem to make me believe I am right.  However, that is the very
reason that I prefer the typedef:  with typedef, you can express these
things quickly and easily.

Reminds me of the old puzzle about casting the number 7 into a pointer to
a function returning a pointer to a pointer to a function returning......

The answer took up about 70 characters on a single line, and looked like
the output from crypt(1).  The puzzle, of course, forbade the use of typedef,
which made it much too easy!-)
-- 
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc at MIT-XX.ARPA

"MU" said the Sacred Chao...



More information about the Comp.lang.c mailing list