Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between const char*, char const*, const char const* & string storage

Tags:

c++

string

First of all, what's the difference between:

(1) const char*
(2) char const*
(3) const char const*

I'm fairly certain I understand this fully, but I'd like someone to give me a sentence for each, specifically, so it sticks in my head. It's one of those things that I'm fine with until someone puts me on the spot and then it gets fuzzy!

Also, how are string-literalls stored by the compiler? This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

like image 690
John Humphreys Avatar asked Jul 27 '11 21:07

John Humphreys


People also ask

What is const char * const *?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.

What is the difference between char * and char?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable).

What is the difference between int const * p and const int * p?

Pointer. In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const int* and int const* says that the pointer can point to a constant int and value of int pointed by this pointer cannot be changed.

Why do we use const char?

If you don't have the choice, using const char* gives a guarantee to the user that you won't change his data especially if it was a string literal where modifying one is undefined behavior. Show activity on this post. By using const you're promising your user that you won't change the string being passed in.


2 Answers

(1) const char* 
(2) char const* 

This is a pointer (that you can change) to a char (or multiple chars) that you cannot change. In other words, all string literals.

(3) const char const*

This is doubled up. I think you were trying to go for the third position:

(4) const char * const

which is a pointer that you cannot change, to a char (or multiple chars) that you cannot change. You can use this for global pointers to literals that should not be accidentally changed.

The string literals are going to be (most likely) lumped right after your code, usually in a segment or section called "rodata".

like image 59
dascandy Avatar answered Oct 04 '22 20:10

dascandy


1 and 2 are equivalent, and specify the type of a pointer to a const char. The pointer itself is not const. 3 is invalid, because it repeats "const". It's like saying const const int. The order is not relevant, so it's also like saying int const int.

In C99 it is valid to repeat const like that. But in C++, you cannot repeat it.

Also, how are string-literalls stored by the compiler?

They are stored in an unspecified way. But compilers are allowed to store them in a read-only portion of the program. So you cannot write to string literals. You are guaranteed that they stay allocated through the whole program lifetime (in other words, they have static storage duration).

This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

You should be aware of the subtle differences between C and C++. In C99, like explained above, const const int is allowed. In C89 and C++ it is forbidden. In C++ you can however introduce a redundant const, if applied to a typedef that is itself const:

typedef int const cint;
cint const a = 0; // this const is redundant!

Same goes for template parameters.

like image 37
Johannes Schaub - litb Avatar answered Oct 04 '22 22:10

Johannes Schaub - litb