Hinweis: probleme mit gcc & ANSI-C Strings
-
Servus,
hier eventuell ein kleiner Tip für Neueinsteiger.char *cTitle = "some string"; ... cTitle = "some other string";
solltet ihr hier beim compilieren Fehler bekommen wenn ihr cTitle einen anderen String zuweisen wollt, dann übergebt dem gcc einfach folgenden Parameter:
"gcc -fwritable-strings [alle anderen parameter]"
danach soltle der Fehler behoben sein, ansonsten hilft meistens noch die zusätzliche angabe von "-ansi"
wollte das nur eben los werden, da ich einige kenne die darüber gestolpert sind, wenns uninteressant ist kanns der Mod ja löschen
-
$ cat foo.c #include <stdio.h> int main(void) { char *foo = "42"; foo = "23"; puts(foo); } $ gcc foo.c $ ./a.out 23
Funktioniert wunderbar, auch ohne -fwritable-strings. Die Option bezieht sich auf etwas anderes. man gcc sagt:
-fwritable-strings Store string constants in the writable data segment and don't uniquize them. This is for compatibility with old programs which assume they can write into string constants. Writing into string constants is a very bad idea; ``constants'' should be constant.
Also:
$ cat bar.c #include <stdio.h> int main(void) { char *foo = "42"; *foo = '5'; puts(foo); } $ gcc bar.c $ ./a.out Segmentation fault $ gcc -fwritable-strings bar.c $ ./a.out 52
Die Option hilft also, kaputte Programme "irgendwie" zum laufen zu ueberreden.