What is the difference between char s[] and char *s?

![Cover Image for What is the difference between char s[] and char *s?](https://images.ctfassets.net/4jrcdh2kutbq/Wri2bSmKSobZtQQS31HBf/deb69725cb251bf624de3908c4ecfc9c/Untitled_design__18___1_.webp?w=3840&q=75&fm=webp)
"Char s[] vs Char *s: Decoding the Mysteries of Storage Duration in C"
š Have you ever wondered what the difference is between char s[]
and char *s
in C? š¤ It's a question many developers have asked themselves at some point. In this blog post, we are going to unravel the mysteries behind these two seemingly similar C declarations. š§©
Understanding the Basics
At first glance, both declarations may appear to serve the same purpose of storing a string. However, there is a crucial distinction between the two in terms of storage duration, both at compile and run time. Let's dive deeper into each declaration to gain a clearer understanding. š”
Declaration 1: char s[] = "hello";
In this declaration, we are using an array to store the string "hello". By specifying char s[]
, we are telling the compiler to allocate memory for the string and copy its contents into the allocated memory. š²
Here's what happens:
The compiler determines the length of the string "hello" (including the null terminator "\0") and allocates the appropriate amount of memory.
The string "hello" is then copied into the allocated memory.
The array
s
now holds a copy of the string "hello" that can be modified. šļø
Declaration 2: char *s = "hello";
In this declaration, we are using a pointer to store the string "hello". By specifying char *s
, we are telling the compiler to create a pointer variable and make it point to the string literal "hello". šÆ
Here's what happens:
The compiler allocates memory to store the string literal "hello".
The pointer
s
is assigned the memory address where the string literal "hello" resides.The pointer
s
now points to the string literal "hello" that cannot be modified. ā
The Difference: Modifiability and Storage Duration
The key difference between char s[]
and char *s
lies in their modifiability and storage duration.
char s[]
creates an array with a copy of the string, allowing us to modify its contents. The memory allocated for this array exists until the end of its scope, such as the end of a function or the closing bracket of a block. This means that any changes made to the array within its scope will persist. āļøOn the other hand,
char *s
creates a pointer that points to a string literal that cannot be modified. The memory allocated for the string literal exists throughout the entire program's execution. Any attempt to modify the string literal will result in undefined behavior. ā ļø
Common Issues and Easy Solutions
Now that we understand the difference between char s[]
and char *s
, let's address some common issues developers may encounter. Here are a few scenarios and their solutions:
Issue 1: Attempting to Modify a String Literal
char *s = "hello";
s[0] = 'H';
š” Solution: Instead of using char *s = "hello";
, use char s[] = "hello";
to create a modifiable copy of the string.
Issue 2: Trying to Assign a New Value to a Pointer
char *s = "hello";
s = "world";
š” Solution: Instead of char *s = "hello";
, use char s[] = "hello";
to create an array that can be modified and assign the new value to the array directly.
š£ Join the Conversation!
Learning the difference between char s[]
and char *s
in C can save you from potential bugs and undefined behavior. If you have any questions or experiences related to this topic, we'd love to hear from you! Share your thoughts in the comments below and let's start a conversation! š¬š
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
