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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What is the difference between char s[] and char *s?

"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:

  1. The compiler determines the length of the string "hello" (including the null terminator "\0") and allocates the appropriate amount of memory.

  2. The string "hello" is then copied into the allocated memory.

  3. 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:

  1. The compiler allocates memory to store the string literal "hello".

  2. The pointer s is assigned the memory address where the string literal "hello" resides.

  3. 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my