char *, const char *, const * char, and const char * const in C3 min read

Not really straight like many high languages such as JavaScript, or Java, working with Strings in C is not so easy-as-pie and sometimes it’s kind of confusing. There are some situations when working with strings you need an extra qualifier such as the const qualifier to make string immutable. However, depends on the place you put the const keyword and it will have a different interpretation. There are 3 confusing combinations which make us feel ambiguous, const char *, const * char, and const *char const, let’s eliminate the syntax confusion and understand the difference between them.

char * – A mutable pointer to mutable character/string

First off, we can declare a mutable character/string in C like this:

char *str; // a pointer points to a mutable character/string.

Later on, we can assign this pointer to an address, and modify the string/character through our str pointer:

#include <stdio.h>
int main() {
  char *str;
  char hello[] = "Hello";
  str = hello;
  printf("%s\n", str); // Hello
  str[4] = '!';
  printf("%s", str); // Hell!
}

However, you cannot directly assign to our str a string literal because it will be flagged as read-only memory, hence any change to the string literal is illegal and segment faults will occur:

#include <stdio.h>
int main() {
  char *str = "Hello";
  printf("%s\n", str);
  str[4] = '!';
  printf("%s", str); // Segmentation fault
}

const char * – Mutable pointer to an immutable string/character

The const keyword, in this case, will make a string/character immutable, meaning its content is read-only, any attempt to modify the original string/character will throw an error:

#include <stdio.h>
int main() {
  const char *str;
  char hello[] = "Hello";
  str = hello;
  str[4] = '!'; // error
  printf("%s", str);
}

However, you still can change the pointer to a different location:

#include <stdio.h>
int main() {
  const char *str;
  char hello[] = "Hello";
  str = hello; // points to the address of the first element of the string "Hello"
  printf("%s\n", str); // Hello
  char hi[] = "Hi";
  str = hi; // now points to a different address
  printf("%s", str); // Hi
}

char * const – Immutable pointer to a mutable string

While const char * makes your string immutable and the pointer location still can flexibly change, char * const is the reversion.

You can essentially change the content of a string/character which pointed to by char * const, but the pointer’s location cannot be changed:

#include <stdio.h>
int main() {
    char hello[] = "Hello";
    char * const str = hello;
    printf("%s\n", str); // Hello
    str[4] = '!';
    printf("%s", str); // Hell!

    char hi[] = "Hi";
    str = hi; // error! change the pointer points to a different location
}

const char * const – Immutable pointer to an immutable string/character

What if you want to fix the content of a string/character and a constant pointer as well? Here we can use const char * const to make that happens:

#include <stdio.h>
int main() {
    char hello[] = "Hello";
    const char * const str = hello; 
    str[4] = '!'; // error, attempt to modify the string
    char hi[] = "Hi";
    str = hi; // error, attempt to reassign the pointer to a different location.
}

So in this case, the str pointer can only point to the address of the string hello, and the content of hello is perpetual and cannot be modified through the pointer str.

Summing up

char *strThe address of this pointer can be changed and the content of the string it points to can be changed through this pointer as well.
const char *Only the address of this pointer might be changed, the content of the string it points to cannot be changed.
char * constThe content of the string this pointer points to can be changed, but the address of this pointer cannot be changed.
const char * constBoth the location of this pointer and the content of the string it points to cannot be changed.

Previous Article

Categories

Archives