Liste des Groupes | Revenir à cl c |
Bart <bc@freeuk.com> writes:What's the difference between such an object, and an array like one of these:On 01/08/2024 09:38, Richard Harnden wrote:I'm fairly sure it is.On 01/08/2024 09:06, Mark Summerfield wrote:>This program segfaults at the commented line:text is pointing to "this is a test" - and that is stored in the
>
#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);
}
program binary and that's why can't modify it.
That's not the reason for the segfault in this case.
With someA string literal creates an array object with static storage duration.
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.)
>
It segfaults when the string is stored in a read-only part of the binary.
Any attempt to modify that array object has undefined behavior.
Les messages affichés proviennent d'usenet.