Fix “assignment of read-only variable” in C
C distinguishes between a read-only pointer variable (`int * const p`, which triggers `assignment of read-only variable 'p'` when reassigned) and a pointer to read-only data (`const int *p`, which triggers `assignment of read-only location '*p'` when dereferenced on the left of `=`).
Quick Fix Summary
Copy into a local mutable variable or buffer when transformation is needed, keep const on input-only parameters, and never cast away const to mutate string literals or const-defined objects (which is undefined behavior).
Why This Error Happens
Reading const declarations clockwise / right-to-left
const char *s means pointer to const char (mutable pointer, read-only characters). char * const s means const pointer to char (fixed address, mutable characters). const char * const s locks both.
Why casting away const crashes on string literals
String literals like "abc" live in read-only .rodata segments; casting (char *)"abc" silences the compiler only to crash with SIGSEGV at runtime. Use char buf[] = "abc"; for writable characters.
Top Root Causes & Specific Code Fixes
Mutating characters through const char * instead of a char[] buffer
`const char *` points to read-only character storage.
void upper_first(const char *s) {
s[0] = 'A';
}void upper_first(char *s) {
s[0] = 'A';
}
char word[] = "apple";
upper_first(word);Incrementing a const-qualified loop bound or parameter
Variables marked `const` can be initialized once, never reassigned.
const int limit = 10;
limit += 5;int limit = 10;
limit += 5;Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
int main(void) {
const char *label = "draft";
label[0] = 'D';
printf("%s\n", label);
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
int main(void) {
char label[] = "draft";
label[0] = 'D';
printf("%s\n", label);
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
DraftDebugging Techniques and Commands
gcc -std=c17 -Wall -Wwrite-strings main.c -o app
Treats string literals as `const char[]` so accidental writes fail at compile time instead of segfaulting.
gcc -std=c17 -Wcast-qual main.c -o app
Flags unsafe pointer casts that strip `const` qualifiers.
Frequently Asked Questions
Is const char *p the same as char const *p in C?
Yes. Both place `const` before `*`, meaning a mutable pointer to read-only `char` elements. Only `char * const p` (with `const` after `*`) makes the pointer itself read-only.
What happens if I cast away const and modify a const global variable?
ISO C17 §6.7.3 specifies that modifying an object defined with a `const`-qualified type through a non-const lvalue has undefined behavior and typically faults in `.rodata`.
Why use char s[] = "hi" instead of char *s = "hi"?
`char s[] = "hi"` copies the 3 bytes (`'h'`, `'i'`, `'\0'`) into a writable stack array you can safely edit in place.