Declaration of String in C

Muiru Jackson
5 min readMar 27, 2023

In C, char [] and char * are both used to represent strings. However, there are some important differences between the two:

  1. Memory allocation: When you declare a char [], memory is allocated for the array to hold the characters in the string. On the other hand, when you declare a char *, no memory is allocated for the string itself. Instead, the pointer variable points to an existing string stored in memory.
  2. Size: When you declare a char [], you need to specify the size of the array in advance. This means that the array can hold a fixed number of characters. In contrast, a char * can point to a string of any length.
  3. Mutable/immutable: A char [] can be modified, while a char * that points to a string literal is typically immutable. Attempting to modify a string literal pointed to by a char * results in undefined behavior.
  4. Initialization: A char [] can be initialized using a string literal, like "hello", or by assigning each character individually. A char * can be initialized using a string literal or by assigning a pointer to an existing char [].
  5. Passing to functions: When you pass a char [] to a function, the entire array is passed, and any modifications to the array within the function persist outside of the function. When you pass a char * to a function, only the pointer is passed by value, so any modifications made to the pointed-to string within the function are local to the function and do not persist outside of it.

In summary, char [] and char * are both used to represent strings in C, but they have different memory allocation, size, mutability, initialization, and function parameter passing behavior.

The code below will help you understand the concept well:

char* str = "Hello"; /* this string is a read only */

The declaration char* str creates a pointer variable named str that can store the memory address of a character array or string.

In the line of code char* str = "Hello";, a string literal "Hello" is assigned to the str pointer variable. A string literal is a sequence of characters enclosed in double quotes, and it is automatically null-terminated with a null character '\0'.

When a string literal is assigned to a character pointer variable, the compiler automatically creates a character array that contains the string literal and assigns the memory address of the first element of the array to the pointer variable.

So in the code char* str = "Hello";, the string literal "Hello" is automatically converted to a character array of size 6 (5 characters for the string and 1 null character) and the memory address of the first character 'H' is assigned to the str pointer variable.

After the assignment, the str pointer variable points to the first character of the string "Hello" and can be used to access and manipulate the characters of the string.

Note that modifying the contents of a string literal through a pointer that points to its memory location is undefined behavior and can cause unexpected program behavior or crashes. To avoid this, it’s recommended to use character arrays instead of string literals if you need to modify the contents of a string in your program. Here is an example code snippet that demonstrates the undefined behavior of modifying a string literal through a pointer:

#include <stdio.h>

int main() {
char* str = "Hello";
printf("Before: %s\n", str);
*(str + 1) = 'a'; // Modify the second character of the string
printf("After: %s\n", str);
return 0;
}

In this example, the str pointer variable is initialized with the string literal "Hello". The program then attempts to modify the second character of the string by accessing it through the pointer using the expression *(str + 1). This expression adds 1 to the memory address pointed to by str to access the second character of the string, and then the dereference operator * is used to access the value stored in that memory location.

However, since string literals are stored in read-only memory, attempting to modify the contents of a string literal through a pointer that points to its memory location is undefined behavior. When the program is run, it may crash, produce unexpected results, or behave in an unpredictable manner.

In this case, when the program attempts to modify the second character of the string “Hello”, it may cause a segmentation fault or access violation error, or the program may produce unexpected output or behavior.

It’s important to note that modifying a string literal through a pointer is a common programming mistake that can lead to difficult-to-debug errors. To avoid this, it’s recommended to use character arrays instead of string literals if you need to modify the contents of a string in your program.

If you need to modify the contents of a string, you should use a character array instead of a string literal. A character array is a sequence of characters that can be modified by your program.

Here is an example code snippet that demonstrates how to modify a string using a character array:

#include <stdio.h>

int main() {
char str[] = "Hello";
printf("Before: %s\n", str);
str[1] = 'a'; // Modify the second character of the string
printf("After: %s\n", str);
return 0;
}

n this example, a character array str is initialized with the string "Hello". The program then modifies the second character of the string by accessing it through the array using the expression str[1]. This expression accesses the second character of the string directly using array indexing.

Since str is a character array, it can be modified by the program without any issues. When the program is run, it will output:

Before: Hello
After: Hallo

As you can see, the second character of the string has been modified from ‘e’ to ‘a’.

By using a character array instead of a string literal, you can modify the contents of a string safely and avoid undefined behavior.

--

--

Muiru Jackson

I am a Software Engineer with a strong background in CS. I believe in continuous learning & knowledge-sharing to improve software development practices.