In C programming, the string is the data type that stores the set of characters terminated with a null character. There are different ways to represent the string in C including char s[] and char*s. Both of them are used to represent strings, although they differ slightly in how memory is allocated. The s[] is an array and *s is a pointer, we will see the major differences between them in this guide.
char s[] in C
In C language, the string is the one-dimensional array of characters, with each character in the array being stored in consecutive memory addresses that can be accessed by its index number. The declaration of a string in C can be done using the char keyword followed by the array name and its size in square brackets, as shown below:
Or:
Here the compiler allocates a fixed amount of memory to store the characters, and the null character terminates the array. The number of characters including the null terminator determines the size of an array. Note that the size of an array must be large enough to store the entire string, if the size of the array is smaller than the string, you might encounter an error.
char *s in C
The * is the pointer in C and basically is a variable that holds a memory address. It is used to point the first letter of the string in C. The following is an example of initializing the pointer in C:
Here the compiler allocates the memory to store the string LINUXHINT in the read-only part of the memory and *s will point to the memory address of the first character of the string.
Key Differences Between char s[] and char *s in C
The main difference between char s[] and char *s is how you handle them once you create them. Both of them store data in memory, but there is a fundamental difference between them. With char *s, you assign a value to a pointer, which is a variable, while char[] is an array that is not a variable. The below table shows the differences between char s[] and char *s in C.
char s[] | char *s |
---|---|
The s[] is an array. | The char *s is a pointer. |
We can edit the char arrays. | We can edit the memory pointed by the pointer. |
s++ is an invalid operation. | s++ is a valid operation and increments the memory address. |
If char s[20], its size is 20 bytes. | sizeof(*s) is 4 bytes or 8 bytes depending on the platform. |
The total string is stored in the stack section. | The pointer is stored in the stack section and points to the memory where the string is stored. |
Example 1
In the following example, we have initialized the char s[], the array of characters. Then we modified the array by replacing the character h located at the index 6 with the character H and printed the modified string.
Example 2
In the following example, we initialized the char *s string and then try to modify it. The segmentation fault error will be displayed on your string because we cannot modify the pointer values once we initialized them:
To correct the above code, you will need to allocate memory for the string and then copy the initial string into it. The strcpy() and malloc() functions can be used for this purpose.
Bottom Line
In C there are multiple ways to declare and initialized the string. However, there are a few differences between them depending on the way you treat them after initialization. The char s[] is an array of strings that can be initialized and modified and the char *s is used to point the string but cannot be modified. The size of char s[] is determined at compiled time and the size of char *s is determined at run time.