C
Charlie Zender
Hi,
I am unable to define an acceptable function argument to qsort() that
does not trigger compiler warnings.
The following function, compare_ints(), compares two integers and returns
an integer [<,=,>] 0 iff val_1 [<,==,>] val_2.
This code is stolen from http://www.cplusplus.com/ref/cstdlib/qsort.html.
The function is used as an argument to ANSI C qsort() routine in stdlib.h.
int
compare_ints(const void *val_1,const void *val_2)
{
return *(int *)val_1-*(int *)val_2;
}
However, GCC 3.3.1 produces this warning when it compiles compare_ints()
with all paranoid, pedantic debugging switches activated (see below):
foo.c: In function `compare_ints':
foo.c:4: warning: cast discards qualifiers from pointer target type
1. How may this comparison function be re-written so that it does not
trigger this warning?
Changing the function prototype is not acceptable, since the function
must be a valid argument to qsort(). However, any method of changing
the function contents, while preserving its functionality, is fair
game. The same problem afflicts the analogous comparison functions I
have for comparing char's, strings, structures.
2. What exactly is unsafe or dangerous in the existing definition?
i.e., what causes the warning in the first place?
Any help appreciated,
Charlie
The exact compile command is
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.
I am unable to define an acceptable function argument to qsort() that
does not trigger compiler warnings.
The following function, compare_ints(), compares two integers and returns
an integer [<,=,>] 0 iff val_1 [<,==,>] val_2.
This code is stolen from http://www.cplusplus.com/ref/cstdlib/qsort.html.
The function is used as an argument to ANSI C qsort() routine in stdlib.h.
int
compare_ints(const void *val_1,const void *val_2)
{
return *(int *)val_1-*(int *)val_2;
}
However, GCC 3.3.1 produces this warning when it compiles compare_ints()
with all paranoid, pedantic debugging switches activated (see below):
foo.c: In function `compare_ints':
foo.c:4: warning: cast discards qualifiers from pointer target type
1. How may this comparison function be re-written so that it does not
trigger this warning?
Changing the function prototype is not acceptable, since the function
must be a valid argument to qsort(). However, any method of changing
the function contents, while preserving its functionality, is fair
game. The same problem afflicts the analogous comparison functions I
have for comparing char's, strings, structures.
2. What exactly is unsafe or dangerous in the existing definition?
i.e., what causes the warning in the first place?
Any help appreciated,
Charlie
The exact compile command is
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.