On 1 Dec 2004 19:32:13 -0800
Charlie Gordon said:
Gregg Woodcock said:
My compiler (m68k-gcc) does not allow this (simplified example;
not my real code) and I don't see why not:
You could have explained what you intended in this code, I will try
and guess for now...
{
char *arrayCharPtr1[] = {"a", "ab", NULL};
char *arrayCharPtr2[] = {"A", "AB", NULL};
You should use const char * here because
arrays hold pointers to constant string literals.
Normally I'd agree but I am programming for Palm (that explains the
StrCopy instead of strcopy which are essentially the same) and on the
Palm, declaring something as const causes the compiler to put it into
the local static code segment which, if it is not code segment #0 (in
my case it will not be) means it cannot be accessed by any other code
segment (which I require). This is certainly one good way to make it
unwritable!
As long as you are only accessing it for reading!
char *arrayCharPtrVar[] =
((someBoolean) ? arrayCharPtr1 : arrayCharPtr2); // <-error
here!
Try this : char **arrayCharPtrVar = someBoolean ? arrayCharPtr1 :
arrayCharPtr2; Use const char **... if you followed my advice above.
I should have mentioned I tried this too and it was not acceptible to
the compiler. However I just realized my mistake: I was compiling
pedantic and this was not an error but a WARNING! When I realized
this, I fixed it right away by casting like this:
char **arrayCharPtrVar =
((someBoolean) ? (char **)arrayCharPtr1 : (char **)arrayCharPtr2);
The warning doesn't meen you need a cast, it meens the variable is of
the wrong type somewhere, either that or the compiler is broken or you
are not doine what you show.
markg@brenda ~ $ cat t.c
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *arrayCharPtr1[] = {"a", "ab", NULL};
char *arrayCharPtr2[] = {"A", "AB", NULL};
int someBoolean = 0;
char **arrayCharVarPtr =
(someBoolean) ? arrayCharPtr1 : arrayCharPtr2;
return 0;
}
markg@brenda ~ $ gcc t.c -ansi -pedantic -Wall -O
t.c: In function `main':
t.c:8: warning: unused variable `arrayCharVarPtr'
markg@brenda ~ $
Not that gcc defines what is acceptable, but it is pretty good about
complaining about producing required diagnostics for incompatible
pointer types.
I *strongly* suggest you try running the above code as is through your
compiler with the pedantic switch and if it compiles OK go back to your
code and see what you have done differently.