Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/3/84; site aplvax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!decuac!aplvax!ded From: ded@aplvax.UUCP (Don E. Davis) Newsgroups: net.lang.c Subject: Casting for a fish that never arrived Message-ID: <864@aplvax.UUCP> Date: Mon, 13-May-85 14:46:14 EDT Article-I.D.: aplvax.864 Posted: Mon May 13 14:46:14 1985 Date-Received: Wed, 15-May-85 01:48:22 EDT Organization: JHU/Applied Physics Lab, Laurel, MD Lines: 79 Here is a problem which cropped up recently. I had two two dimensional arrays of pointers to character strings (as represented by Cwords and Pwords below). I wanted to point to either Cwords or Pwords and be able to flip back and forth between two lists of strings by indexing. For example, if Words pointed to Cwords, then Words[0][0] would point to "hello" and Words[1][0] would point to "goodbye". If Words pointed to Pwords the two choices would be "foo" and "foobar". The only problem is, I had a hard time getting this to work. What I needed was something to put in the place of CASTING EXPRESSION below. Apparently there is no way to perform this kind of cast (the array's second dimension is the problem). Of course, I could have used pointer arithmetic (e.g., printf("%s\n",*(Words+i*4+j)), but wound up using a typedef instead since the WORD[][] format made my code a lot cleaner. What do you say, gang, is there some CASTING EXPRESSION I could have used? /*-----------------------------------------------------------------------*/ 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; main() { int i,j; Words = (char *)Cwords; for (i=0; i<2; i++) for (j=0; j<2; j++) printf("%s\n",(CASTING EXPRESSION)Words[i][j]); Words = (char *)Pwords; for (i=0; i<2; i++) for (j=0; j<2; j++) 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] = { {"hello","bye","go","come"}, {"say","can","you","see"} }; SYNTAX Pwords[2] = { {"foo","foo","foo","foo"}, {"foo","foo","foo","foo"} }; SYNTAX *Words; main() { int i,j; Words = (SYNTAX *)Cwords; for (i=0; i<2; i++) for (j=0; j<2; j++) printf("%s\n",Words[i][j]); Words = (SYNTAX *)Pwords; for (i=0; i<2; i++) for (j=0; j<2; j++) printf("%s\n",Words[i][j]); } -- Don Davis JHU/APL ...decvax!harpo!seismo!umcp-cs!aplvax!ded ...rlgvax!cvl!umcp-cs!aplvax!ded