C语言字符串字面量数组中两个const关键字的作用验证问询
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
constapplies to the array elements themselves (eachchar*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
constapplies to the content that each pointer points to (thecharvalues). 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[]:- Right
const: You can't change where the pointers in the array point (e.g.,myArray[0] = "new"is invalid). - Left
const: You can't use those pointers to modify the data they reference (e.g.,myArray[0][0] = 'U'is invalid).
- Right
内容的提问来源于stack exchange,提问作者andreipb

