Liste des Groupes | Revenir à cl c |
On Thu, 01 Aug 2024 08:06:57 +0000Your suspicions are correct - in C, string literals are used to initialise an array of char (or wide char, or other appropriate character type). Perhaps you are thinking of C++, where the type is "const char" (or other const character type).
Mark Summerfield <mark@qtrac.eu> wrote:
This program segfaults at the commented line:The answers to your question are already given above, so I'd talk about
>
#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);
}
>
something else. Sorry about it.
To my surprise, none of the 3 major compilers that I tried issued the
warning at this line:
char* text = "this is a test";
If implicit conversion of 'const char*' to 'char*' does not warrant
compiler warning than I don't know what does.
Is there something in the Standard that explicitly forbids diagnostic
for this sort of conversion?
BTW, all 3 compilers issue reasonable warnings when I write it slightly
differently:
const char* ctext = "this is a test";
char* text = ctext;
I am starting to suspect that compilers (and the Standard?) consider
string literals as being of type 'char*' rather than 'const char*'.
Les messages affichés proviennent d'usenet.