相关疑难解决方法(0)

字符串文字:指针与字符数组

在这个声明中:

char *a = "string1"
Run Code Online (Sandbox Code Playgroud)

究竟什么是字符串文字?是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;
} 
Run Code Online (Sandbox Code Playgroud)

请添加除上述要点之外的一些细节.谢谢.

调试输出显示错误 a[0] = 'Y';

Reading symbols from /home/jay/Desktop/MI/chararr/a.out...done.
(gdb) b main
Breakpoint 1 at 0x40056c: …
Run Code Online (Sandbox Code Playgroud)

c segmentation-fault string-literals

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

segmentation-fault ×1

string-literals ×1