Pour changer, je plussoie ce que vient de dire kwariz.
J'ai retrouvé un warning que j'avais découvert ici (il me semble que c'était par gangsoleil) : -Wwrite-strings.
Exemple de son effet avec le code suivant, trouvé sur Stackoverflow :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void somefunc(char buffer[10]) {
int i;
for (i = 0; i < 10; i++)
buffer[i] = 0;
}
int main(void) {
somefunc("Literal");
return 0;
} |
Sans l'option, il compile sans erreur ou warning. Avec, on a un warning :
D:\...\main.c|14|warning: passing argument 1 of 'somefunc' discards 'const' qualifier from pointer target type [enabled by default]|
C'est bien car on va essayer d'écrire dans un string litteral ce qui va forcément pas bien marcher (doux euphémisme pour dire qu'il va y avoir une erreur de segmentation).
Son effet est subtil et la documentation de gcc conseille de ne l'utiliser que si on est minutieux (mais j'ai pas encore trop vu pourquoi ^^) :
-Wwrite-strings
When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer produces a warning. These warnings help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it is just a nuisance. This is why we did not make -Wall request these warnings.
Partager