You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言字符串字面量数组中两个const关键字的作用验证问询

Clarifying the Two const Qualifiers in Your C Array Initialization

Great question! Let's break down exactly what each const does, and give you a clearer example to see the first one in action—since string literals can complicate things with their read-only memory behavior.

First, let's parse the declaration properly:

const char* const myArray[] = { "one", "two", "three" };

We can split this into two separate const constraints:

  • The rightmost const applies to the array elements themselves (each char* pointer). As you already figured out, this means you can't reassign where any element points—myArray[0] = "uno" will fail to compile, which you've tested.
  • The left const applies to the content that each pointer points to (the char values). This means you cannot modify the data referenced by those pointers through the pointers themselves.

The Problem with String Literals

Your test with myArray[0][0] = 'u' hits a quirk: string literals are stored in read-only memory on most systems. Even if you remove the left const, the code might compile, but modifying the literal will trigger undefined behavior (usually a crash). This makes it hard to see the left const's actual compile-time role.

A Better Example to Show the Left const's Effect

Let's use modifiable character arrays (instead of string literals) to demonstrate the left const clearly. These arrays live in writable memory, so we can see exactly when the const stops us from making changes:

Case 1: Without the left const (allowed to modify content)

#include <stdio.h>

int main() {
    // These are modifiable character arrays (stored on the stack, not read-only)
    char str1[] = "one";
    char str2[] = "two";
    char str3[] = "three";

    // No left const: pointers can modify the content they point to
    char* const myArray[] = { str1, str2, str3 };
    
    // This works perfectly—we're modifying the writable array via the pointer
    myArray[0][0] = 'U';
    printf("Modified string: %s\n", myArray[0]); // Outputs "Uone"

    return 0;
}

Case 2: With the left const (compile-time block on modifying content)

Now add the left const and try the same modification:

#include <stdio.h>

int main() {
    char str1[] = "one";
    char str2[] = "two";
    char str3[] = "three";

    // Left const added: pointers cannot modify the content they point to
    const char* const myArray[] = { str1, str2, str3 };
    
    // This will FAIL to compile immediately!
    myArray[0][0] = 'U'; // Error: assignment of read-only location '*myArray[0]'

    return 0;
}

Key Takeaway

The left const is a compile-time guard: it tells the compiler "don't let anyone use these pointers to write to the memory they point to". It doesn't care if the underlying memory is actually writable (like our stack arrays) or read-only (like string literals)—it just prevents you from attempting the write through the pointer.

To recap both consts:

  • const char* const myArray[]:
    1. Right const: You can't change where the pointers in the array point (e.g., myArray[0] = "new" is invalid).
    2. Left const: You can't use those pointers to modify the data they reference (e.g., myArray[0][0] = 'U' is invalid).

内容的提问来源于stack exchange,提问作者andreipb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:23:41