Liste des Groupes | Revenir à cl c |
On 01/08/2024 09:06, Mark Summerfield wrote:That's not the reason for the segfault in this case. With some compilers, you *can* modify it, but that will permanently modify that string constant. (If the code is repeated, the text is already in capitals the second time around.)This program segfaults at the commented line:text is pointing to "this is a test" - and that is stored in the program binary and that's why can't modify it.
>
#include <ctype.h>
#include <stdio.h>
>
void uppercase_ascii(char *s) {
while (*s) {
*s = toupper(*s); // SEGFAULT
s++;
}
}
>
int main() {
char* text = "this is a test";
printf("before [%s]\n", text);
uppercase_ascii(text);
printf("after [%s]\n", text);
}
Les messages affichés proviennent d'usenet.