Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why char *s = "hello"; is allowed?

Tags:

c

pointers

char *s = "hello";

The code above allocates 6 bytes in read-only section of a program (I've forgot the name of the section) to store the string hello. Then, s is initialized to point to the first character of the string hello. Modifying the string "hello" is undefined behavior. Besides, "hello" itself is constant in nature. The program doesn't have permission to change read-only section.

I'm using MS VC++ 2010 Express. My question is, why does the compiler allows s, which is a char *, to point to the constant string? Shouldn't there be a compiler error? Shouldn't the compiler force us to use const char *s = "hello"; instead of char *s = "hello";?

Thanks.

like image 706
Donotalo Avatar asked Nov 18 '11 08:11

Donotalo


People also ask

What is the meaning of char * s?

char *s = "hello"; A string literal is used to create these character blocks somewhere in the memory which this pointer s is pointing to. We can here reassign the object it is pointing to by changing that, but as long as it points to a string literal the block of characters to which it points can't be changed.

What does char * s mean in C?

Difference between char s[] and char *s in CThe s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.

What is difference between char * and char []?

What is the difference between char and char*? char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location.

What is * a [] in C?

char *A[] is an array, and char **A is a pointer. In C, array and pointer are often interchangeable, but there are some differences: 1. with "char *A[]", you can't assign any value to A, but A[x] only; with "char **A", you can assign value to A, and A[x] as well.


2 Answers

In C, "hello" has type char[]. This is not C++. See this question.

like image 174
Pascal Cuoq Avatar answered Sep 20 '22 18:09

Pascal Cuoq


This predates the times when the const qualifier was introduced to C. The C standards body is very conservative with respect to existing code. Any improvement of the language is required to be done in such a way that it wouldn't break any existing conforming code that is written for the previous version of the standard.

If such things then lead to undesirable complications, the feature is then deprecated and maybe changed years after that.

For the concrete feature of string literals being typed char[] instead of char const[], yes this is unfortunately a pitfall for beginners. Just get the habit from the start to use char const* whenever you reference such strings.

Edit: For the question if a compiler could or should warn about that, I think this is just difficult to trace. In this code

int main(void) {
  "hello"[0] = 'H';
  char * a = "hoho";
  a[0] = 'H';
}

gcc only gives me a warning on the first assignement, not on the second. clang doesn't capture it at all.

like image 23
Jens Gustedt Avatar answered Sep 21 '22 18:09

Jens Gustedt