Home c++ Assignment of the Const Char *

Assignment of the Const Char *

Author

Date

Category

Good afternoon, I want to place the point over “I” for myself.

const char * v = "123d";
  Const char * c = "123";
  C [2] = '5'; // You can not do it this way
  C = V;
  C = "ASDFAS";

Question next. Why, when we const char * We assign a string, it assigns, but no symbol? Maybe somewhere for Const char * the assignment operator is overloaded, and he assigns a new address? And when applied literal, we are trying to write down the same address?


Answer 1, Authority 100%

Question next. Why, when we const char * We assign a string, it assigns, but no symbol?

You need to distinguish the constant pointer and pointer to the constant.

1) Pointer to the constant: You can not change the contents of the object to which the pointer specifies, but you can change the contents of the pointer itself (the pointer is a variable containing the address of another variable).

char array [] = "string";
Const char * c = array; // pointer to an object that can not be changed
C [1] = 'A'; // You can not, because the contents of the specified object is changing
C = "345"; // You can, because the value of the pointer itself is changing

2) Constant pointer: You can change the contents of the object, but you cannot 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 c = array;
C [1] = 'a'; // can
C = "345"; // it is forbidden

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

const char * const c = "123";

Answer 2, Authority 73%

After assigning Const char * c = "123"; in C a certain address is stored for which the string “123” is placed. The compiler usually places it in memory, which is “Read only” (you can do it there, simply the operating system knows that it is incorrect and stops writing an entry). But since it will use only for reading, then everything is fine.

The c [2] = '5' line; tries to write to this unchangeable memory. Read only reading.

Why is it done? For optimizations and simplification. Let in your program there are still ten places where there is a constant “123”. The compiler will not create 11 copies, and will use the same address. And now imagine that the above change is allowed :).

Old compilers did not do this and often experienced interesting special effects when the constants changed their meaning.

But the compiler can come and otherwise.

const char * test = "test111";
PrintF (Test);

In this case, it may even throw the TEST variable, and its value is displayed directly.

Assigning C = V; Allowed, since the data on the pointer does not change, but only he himself. And the pointer is not constant. Similarly, with assignment C = "ASDFAS"; .


Answer 3, Authority 45%

Programming textbook on C++ says:

Official type of string literals, such as “Hi”, – Const Char [] . The “Hi” string has the type const char [7] . Secret! After all, in the word hello six letters, and not seven! Do not worry, there are no errors here. The optional element contains the final empty character, ‘\ 0’ (value 0), which tells the line of the string length. At the end of each string literal there is a hidden empty symbol (\ 0), which allows algorithms to determine the length of the string and the fact of achieving the end of the line. String length is not necessary for all
Algorithms, but most of them. Assign a string literal of a variable type char * can be as follows:

char * x = "hello";

However, with a value that X indicates, it is impossible to work. If it
will not be constant, an error will arise as in the following example:

* x = 't';

This code causes an error because it is not possible to change the value of the constant. To get a string that can be changed, you should assign a string literal to a string object or a symbolic array. An example of announcement of a symbolic array, whose elements can change:

char s [] = "hello";
S [0] = 'T';

With this code, everything is in order, and the new row value will be “triper”.
Many of the standard library functions with are taken as one of the arguments the value of type Char *. However, if the string is stored as a pointer to the character, its length cannot be defined, as in the case of string literals ended with an empty symbol. In the absence of an empty symbol, it is impossible to understand where the string ends.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions