Assigning the const char character *

Good afternoon, I want to dot the "I" for myself.

    const char *v = "123d";
    const char *c = "123";  
    c[2] = '5'; // Так нельзя 
    c = v;
    c = "asdfas";

The question is as follows. Why, when we const char * assign a string, does it assign, but a character does not? Maybe the assignment operator is overloaded somewhere for const char *, and it assigns a new address to it? And when assigning a literal, we try to write to the same address?

 5
c
Author: Arhadthedev, 2013-03-15

3 answers

The question is as follows. Why, when we const char * assign a string, does it assign, but a character does not?

You need to distinguish between a constant pointer and a pointer to a constant.

1) Pointer to a constant: you can't change the contents of the object that the pointer points to, but you can change the contents of the pointer itself (a pointer is a variable that contains the address of another variable).

char array[] = "string";
const char * с = array; // Указатель на объект, который нельзя менять
c[1] = 'a';             // Нельзя, т. к. меняется содержимое указываемого объекта
с = "345";              // Можно, т. к. меняется значение самого указателя

2) Constant pointer: you can change the contents of an object, but you can't change the value of the pointer itself. Simply put, the pointer cannot be reassigned to another object, but the pointer itself can be changed.

char array[] = "string";
char * const с = array;
c[1] = 'a';    // можно
с = "345";     // нельзя

3) Constant pointer to a constant: the set of the first two.

const char * const с = "123";
 11
Author: fogbit, 2016-04-26 10:53:40

After assigning const char *c = "123"; , c stores an address that contains the string "123". The compiler usually places it in memory, which is "read only" (you can write something there, just the operating system knows that it is wrong and stops writing attempts). But since it will be read-only, everything is fine.

The string c[2] = '5'; tries to write to this immutable memory. Read-only memory.

Why is this done? for optimizations and simplification. Let in Your program has ten more places where there is a constant "123". The compiler will not create 11 copies, but will use the same address. Now imagine that the above change is allowed:).

Older compilers did not do this, and often interesting special effects were observed when constants changed their value.

But the compiler can do something else.

const char * test = "test111";
printf(test);

In this case, it can even throw out the test variable, and output its value directly.

Assignment to c = v; is allowed, since the data on the pointer is not changed, but only the pointer itself. And the pointer is not constant. Similarly, with the assignment c = "asdfas";.

 8
Author: KoVadim, 2013-03-15 09:16:36

The C++ programming tutorial says that:

The official type of string literals, such as "Hello", is const char []. The string "Hello" is of type const char [7]. Wait a second! After all, the word Hello has six letters, not seven! Don't worry, there is no mistake here. The optional element contains a trailing empty character, '\0' (value 0), which tells the computer the length of the string. At the end of each string literal, there is a hidden empty character (\0) that allows algorithms to determine the length of the string and whether the end of the string has been reached. Not everyone needs the length of the string algorithms, but most of them. You can assign a string literal to a variable of type char* as follows:

char* х = "Привет";

However, you can't work with the value that x points to. If it is not constant, an error will occur, as in the following example:

*х = 'Т';

This code causes an error because you cannot change the value of a constant. To get a string that can be modified should be assigned a string literal to a string object or a character array. Example of declaring a character array whose elements can change:

char s[] = "Привет";  
s[0] = 'Т';

This code is fine, and the new string value will be "Trivet". Many of the standard C library functions take as one of the arguments a value of the type char*. However, if a string is stored as a pointer to a character, its length cannot be defined, as in in the case of string literals terminated with an empty character. In the absence of an empty character, it is impossible to understand where the string ends.

 5
Author: teanЫЧ, 2013-03-15 09:08:12