Difference between string pointer and array
Filed under Samsung
Free read 2 of 3Points to a string literal residing in read-only data memory. The pointer variable `p` itself can be reassigned to point elsewhere, but modifying the string content causes undefined behavior.
The question
Explain the fundamental differences between a string pointer `char *p = "SAMSUNG";` and a character array `char p[] = "SAMSUNG";` in C.
Constraints
- Must address memory location, modifiability, and sizeof behavior
Examples
- Input
- char *p = "SAMSUNG"; vs char p[] = "SAMSUNG";
- Output
- p[0] = 'X' on char* causes undefined behavior/segfault; on char[] it successfully modifies the stack array.
- Why
- String literals are typically stored in read-only memory sections, whereas array initialization copies the literal into stack/global modifiable memory.
Approach
Points to a string literal residing in read-only data memory. The pointer variable `p` itself can be reassigned to point elsewhere, but modifying the string content causes undefined behavior.
Allocates an array on the stack (or static memory) initialized with a copy of the string literal characters. The array identifier is a fixed address, but its contents are fully modifiable.
`sizeof(p)` for a pointer returns the size of the pointer itself (4 or 8 bytes), while `sizeof(p)` for an array returns the total size of the array including the null terminator.
Use `const char *` when referencing read-only text constants, and `char []` when string manipulation or mutation is required.
Tips
- What they are testing: Check understanding of read-only memory sections versus stack allocation.
- Common mistake: Assuming `char *` allocates a modifiable buffer for the string contents.
Related Samsung questions
Collected from public candidate posts and coding forums. Updated Nov 2026.