この声明では:
char *a = "string1"
文字列リテラルとは正確には何ですか?それstring1
ですか?このスレッドのため、CおよびC ++の文字列リテラルのタイプは何ですか?何か違うことを言う。
私の知る限り
int main()
{
char *a = "string1"; //is a string- literals allocated memory in read-only section.
char b[] = "string2"; //is a array char where memory will be allocated in stack.
a[0] = 'X'; //Not allowed. It is an undefined Behaviour. For me, it Seg Faults.
b[0] = 'Y'; //Valid.
return 0;
}
上記以外の点を追加してください。ありがとう。
デバッグ出力にエラーが表示されます a[0] = 'Y';
Reading symbols from /home/jay/Desktop/MI/chararr/a.out...done.
(gdb) b main
Breakpoint 1 at 0x40056c: file ddd.c, line 4.
(gdb) r
Starting program: /home/jay/Desktop/MI/chararr/a.out
Breakpoint 1, main () at ddd.c:4
4 {
(gdb) n
6 char *a = "string1";
(gdb) n
7 char b[] = "string2";
(gdb)
9 a[0] = 'Y';
(gdb)
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400595 in main () at ddd.c:9