程序在 c 中的多行 printf 之后以分段错误(核心转储)结束?

Program ends in Segmentation fault (core dumped) after multiline printf in c?

我正在尝试学习 C,作为一种爱好。因此,我正在创建一个包含大量声明等的长 .c 文件,以查看和学习编程语言。我的问题是我的程序崩溃了,我不明白为什么!这是代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main() {
    printf("Hello, World!\n");

    double Double_Array[5] = {[0] = 9.0, [2] = 5.0, [1] = 7.12, [4] = 3.E+25};
    double DoubleArray[] = {0.0007, 0.1, 6};

    for(size_t i = 0; i < 5; i++) {
        // %zu and %g are what's called "format specifiers"
        printf("element␣%zu␣is␣%g,␣\tits␣square␣is␣%g\n", i, Double_Array[i], Double_Array[i]*Double_Array[i]);
    }

    int int_x[] = {1,2,3}; // x has type int[3] and holds 1,2,3
    int int_y[5] = {1,2,3}; // y has type int[5] and holds 1,2,3,0,0
    int int_z[3] = {0}; // z has type int[3] and holds all zeroes

    char str_array[] = "abc"; // str has type char[4] and holds 'a', 'b'. 'c', '[=10=]'
    char str_array3[3] = "abc"; // str has type char[3] and holds 'a', 'b', 'c'
    wchar_t wstr[4] = L"猫"; // str has type wchar_t[4] and holds L'猫', '[=10=]', '[=10=]', '[=10=]'

    // Ternary (condition) operation
    int aaa = 10, bbb = 11;
    (aaa > bbb) ? (printf("A is the biggest!\n")) : (printf("B is the biggest!\n"));

    int my_single_array[5] = {1, 5, 2, 4, 7};
    int my_multidimensional_array[2][3][4] = {
            {{9, 1, 8, 3}, {1, 8, 3, 4}, {8, 3, 4, 5}}, // 0
            {{8, 4, 8, 3}, {8, 5, 5, 1}, {9, 6, 8, 3}} // 1
    }; // 3D

    /*
    for(int i = 0; i < 2; i++) {
        printf("%i\n", my_multidimensional_array[i][i][i]);
    }

    printf("\nsingle array: %i", my_single_array[1239]);
    */
    char* char_A = "A";
    const char* char_AA[2] = {"AA"};
    char* const char_AAA[50] = {"AAA"};
    const char* const char_AAAA[50] = {"AAAA"};

    printf("\n\n"
           "char_A: %s\n"
           "char_AA: %s\n"
           "char_AAA: %s\n"
           "char_AAAA: %s\n"
           " \n\n"
           , char_A,char_AA[0], char_AAA[0], char_AAAA[0]);

    printf("got here"); // never prints this CRASHES BEFORE THIS

    char_A[0] = 'B';
    char_AA[0] = "CD";
    // char_AAA = 'Changed char_AAA'; // ILLEGAL, reason: constant content, movable pointer
    // char_AAAA = 'Changed char_AAAA'; // ILLEGAL, reason: constant content and pointer!

    printf("HERE");
}

为什么我会收到分段错误?我是否正在访问我不支持的内存部分?我试过不打印 char_A、char_AA、char_AAA 和 char_AAAA,但尽管没有访问变量,程序仍然在打印后崩溃。

字符串文字在 C 中是不可变的,尽管与 C++ 相反,它们具有非常量字符数组类型。任何修改字符串文字的尝试都会导致未定义的行为。

char* char_A = "A";
//...
char_A[0] = 'B';

来自 C 标准(6.4.5 字符串文字)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

问题出在

的声明上
char* char_A = "A";

这将使 char_A 常量字符串,当您尝试修改它时会发生分段错误。

改为

char char_A[] = "A";

我会根据你的回答回答我自己的问题,因为没有一个答案是完整的。

这样定义字符串文字

char* char_A = "A";

导致不可变(不可变 = 常量 = 无法更改)字符串文字。

我的 printf("got here"); 从不打印的原因是因为它被放在一个待打印的队列中,然后我尝试通过执行 char_A[0] = 'B'; 来更改常量字符串文字。这会导致未定义的行为(在本例中是分段错误,这意味着部分内存被更改或试图更改,从而导致崩溃)。

通过删除 char_A[0] = 'B'; 或将 char* char_A = "A"; 更改为 char char_A[] = "A";,问题已解决!看下面的例子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main() {
    //char* char_A = "A"; // COMMENTED OUT
    char char_A[] = "A"; // NEW
    const char* char_AA[2] = {"AA"};
    char* const char_AAA[50] = {"AAA"};
    const char* const char_AAAA[50] = {"AAAA"};

    printf("\n\n"
           "char_A: %s\n"
           "char_AA: %s\n"
           "char_AAA: %s\n"
           "char_AAAA: %s\n"
           " \n\n"
           , char_A,char_AA[0], char_AAA[0], char_AAAA[0]);

    printf("got here");

    //char_A[0] = 'B'; // COMMENTED OUT
    char_AA[0] = "CD";
    // char_AAA = 'Changed char_AAA'; // ILLEGAL, reason: constant content, movable pointer
    // char_AAAA = 'Changed char_AAAA'; // ILLEGAL, reason: constant content and pointer!

    printf("HERE");
}