What is the difference between char s[] and char *s in C?
In C, char s[]
and char *s
are both used to handle strings (arrays of characters). However, they differ in how the memory is allocated and managed, and what can be done with each.
1. Declaration and Memory Allocation
char s[]
- Automatic Allocation: Declares a fixed-size array of
char
s. If it’s declared as a local variable in a function (and initialized with a string literal), the size is determined by the initializer. - Example:
char s[] = "Hello";
- The size of
s
is automatically 6 ("Hello"
plus the null terminator'\0'
). - Memory is allocated on the stack if it’s a local variable, or in static storage if it’s declared at file scope with static duration.
- The size of
char *s
- Pointer Declaration: Declares
s
as a pointer to one or morechar
s. It does not allocate an array automatically. - Example 1 (Pointing to a string literal):
char *s = "Hello";
s
points to a string literal stored in read-only memory (on most modern compilers).- Attempting to modify this memory (e.g.,
s[0] = 'h';
) results in undefined behavior or a crash.
- Example 2 (Pointer to dynamic allocation):
char *s = malloc(6 * sizeof(char)); strcpy(s, "Hello");
s
points to a dynamically allocated block of memory on the heap.
2. Mutability
char s[] = "Hello";
- The characters are stored in an array that belongs to your program’s writable memory (stack or static region).
- You can modify the content:
s[0] = 'h'; // OK
char *s = "Hello";
(pointing to a string literal)- On most compilers, string literals are stored in read-only memory.
- Attempting to modify results in undefined behavior:
s[0] = 'h'; // Potential crash or segfault
- If
char *s
is pointing to malloc’d memory, then you can modify it as long as the allocated size is sufficient.
3. Size Information
-
char s[]
- The compiler knows the size of
s
at compile time if it’s initialized with a constant string (or if you explicitly specify the size). sizeof(s)
gives the total size in bytes of the entire array, including space for'\0'
if it’s a string.
- The compiler knows the size of
-
char *s
- The compiler only knows
s
is a pointer. sizeof(s)
yields the size of a pointer (e.g., 4 bytes on a 32-bit system, 8 bytes on a 64-bit system), not the size of the data it points to.
- The compiler only knows
4. Example Comparison
#include <stdio.h> #include <string.h> int main(void) { char s1[] = "Hello"; // Array of 6 chars (H,e,l,l,o,\0) char *s2 = "World"; // Pointer to a string literal printf("s1: %s\n", s1); // prints "Hello" printf("s2: %s\n", s2); // prints "World" // Modify s1 - OK s1[0] = 'h'; printf("modified s1: %s\n", s1); // prints "hello" // Modify s2 - undefined behavior if s2 points to read-only memory // s2[0] = 'w'; // <-- can crash or cause data corruption // Print sizes printf("sizeof(s1) = %zu\n", sizeof(s1)); // likely 6 printf("sizeof(s2) = %zu\n", sizeof(s2)); // size of a pointer (e.g., 8 on 64-bit) return 0; }
5. Key Differences Summary
-
Memory Layout
char s[]
: A fixed-size array allocated in local (stack) or static memory.char *s
: A pointer to achar
or array ofchar
s, which may be in read-only storage if it points to a string literal, or in writable memory if it points to a modifiable buffer.
-
Modifiability
char s[] = "text"
: You can modify characters.char *s = "text"
(string literal): Typically cannot modify safely.char *s = malloc(...)
: You can modify if you allocated enough space.
-
Size Awareness
char s[]
: The compiler knows the total size of the array.char *s
: The compiler only knows it’s a pointer; you must keep track of allocated length yourself.
-
Usage Patterns
char s[]
is suitable for small, fixed-size buffers or for local arrays containing string literals that you need to modify.char *s
is used for dynamic memory (usingmalloc
/free
) or pointing to string literals (usually read-only).
Further Learning
If you want to deepen your understanding of memory, pointers, and strings in C (and also become a better problem solver for coding interviews), here are two recommended courses from DesignGurus.io:
-
Grokking Data Structures & Algorithms for Coding Interviews
- Dive deep into fundamentals like arrays, linked lists, dynamic allocation, and memory management in C.
-
Grokking the Coding Interview: Patterns for Coding Questions
- Learn the recurring coding patterns that appear in interviews and real-world scenarios, helping you quickly identify and solve complex problems.
Understanding the nuances between char s[]
and char *s
is crucial to writing safe, efficient, and clear C code—especially when dealing with strings and memory.
CONTRIBUTOR
TechGrind