W
Walter Dnes (delete the 'z' to get my real address
I'm trying to initialize an array of error messages, so that I can
print out an error message by using the 'nth string in an array, e.g.
printf("%s\n", messages[n]);
I'm still hazy on arrays of pointers to strings, so you may want to
finish your drinks before examining my code. Here's a small sample
program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *messages[4] = {"Hell", "o", " ", "world"};
printf("%s%s%s%s%s\n", messages[1], messages[2],
messages[0], messages[2], messages[3] );
return EXIT_SUCCESS;
}
Compiled under gcc in excruciatingly ansi mode (-ansi -pedantic, and a
few other esoteric options as well), it gives...
[20:21:02][/misc/home/user2/tbrg] ./test1
o Hell world
That's what I expected. But the compiler gives the following
warnings, related to the "char *messages[4]" declaration...
test1.c: In function `main':
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
Since gcc's warnings have spotted a few "legal" howlers of mine
already, I take them seriously. What am I doing wrong, and how can I
correct it ?
print out an error message by using the 'nth string in an array, e.g.
printf("%s\n", messages[n]);
I'm still hazy on arrays of pointers to strings, so you may want to
finish your drinks before examining my code. Here's a small sample
program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *messages[4] = {"Hell", "o", " ", "world"};
printf("%s%s%s%s%s\n", messages[1], messages[2],
messages[0], messages[2], messages[3] );
return EXIT_SUCCESS;
}
Compiled under gcc in excruciatingly ansi mode (-ansi -pedantic, and a
few other esoteric options as well), it gives...
[20:21:02][/misc/home/user2/tbrg] ./test1
o Hell world
That's what I expected. But the compiler gives the following
warnings, related to the "char *messages[4]" declaration...
test1.c: In function `main':
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
Since gcc's warnings have spotted a few "legal" howlers of mine
already, I take them seriously. What am I doing wrong, and how can I
correct it ?