C
Charlie Zender
Hi,
I am unable to compile a large body of code with extremely pedantic
compile time checks activate, so that warnings cause errors.
With GCC 3.3.1, I do this with
gcc -std=c99 -pedantic -Wall -Wunused -Werror -W -Wmissing-prototypes
-Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align
-Wwrite-strings -c -o foo.o foo.c
(This is basically what GSL recommendsd for scientific codes.)
The problem is that I have a lot of code that generates these warnings:
foo.c:258: warning: "assignment discards qualifiers from pointer target
type"
This warning occurs when I try to return string literals (e.g., error
messages) to a calling function. Currently, I use code like
char * err_sng(const int err_nbr){
switch(err_nbr){
case 0: return "Message 0";
case 1: return "Message 1";
}
}
1. Say you have a function whose purpose is to return a constant
string literal (e.g., an error message) to a calling function.
How would you write this type of function so as not to generate
"assignment discards qualifiers" warnings?
Note that these warnings disappear if I prototype the function as
const char * err_sng(int err_nbr);
Is that legal? I suspect that qualifying a return value is a GNUism
and am not sure if there is a preferred method that avoids this
dubious prototype.
Any help appreciated,
Charlie
I am unable to compile a large body of code with extremely pedantic
compile time checks activate, so that warnings cause errors.
With GCC 3.3.1, I do this with
gcc -std=c99 -pedantic -Wall -Wunused -Werror -W -Wmissing-prototypes
-Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align
-Wwrite-strings -c -o foo.o foo.c
(This is basically what GSL recommendsd for scientific codes.)
The problem is that I have a lot of code that generates these warnings:
foo.c:258: warning: "assignment discards qualifiers from pointer target
type"
This warning occurs when I try to return string literals (e.g., error
messages) to a calling function. Currently, I use code like
char * err_sng(const int err_nbr){
switch(err_nbr){
case 0: return "Message 0";
case 1: return "Message 1";
}
}
1. Say you have a function whose purpose is to return a constant
string literal (e.g., an error message) to a calling function.
How would you write this type of function so as not to generate
"assignment discards qualifiers" warnings?
Note that these warnings disappear if I prototype the function as
const char * err_sng(int err_nbr);
Is that legal? I suspect that qualifying a return value is a GNUism
and am not sure if there is a preferred method that avoids this
dubious prototype.
Any help appreciated,
Charlie