Liste des Groupes | Revenir à cl c |
On 01/08/2024 21:59, Keith Thompson wrote:This char arrays are modifyable because they're not const.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:>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.
I'm fairly sure it is.
>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.)
>
It segfaults when the string is stored in a read-only part of the binary.
A string literal creates an array object with static storage duration.
Any attempt to modify that array object has undefined behavior.
static char A[100];
static char B[100]={1};
Do these not also have static storage duration? Yet presumably these can be legally modified.
Les messages affichés proviennent d'usenet.