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

标签 c pointers segmentation-fault string-literals

我正在尝试学习 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', '\0'
    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'猫', '\0', '\0', '\0'

    // 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.

关于c - 在 c 中的多行 printf 后,程序以段错误(核心转储)结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917692/

相关文章:

c - scanf 和 fgets 的问题

c - 使用Python返回指向已经存在的内存地址的指针

c - 如何将指针作为参数传递给存储数组地址的地方

c - 用 C 读取文件时出现段错误

c - 发生故障时释放所有分配的内存

c - 为什么一位 `int` 位域的值是 0 和 −1?

c++ - 如何使用 CreateProcess 将输出重定向到文件?

c - 使用接近最大基础整数值的哨兵指针值安全吗?

c++ - 声明 vector 时出现段错误

c - SIGSEGV,(貌似)由 printf 引起