Two tips to share about c Pointers

  • 2020-04-01 23:31:27
  • OfStack

1: constant pointer and pointer constant

Constant Pointers are close to pointer constant names, but they are quite different.
Char const *st[4]=" STR "; Const char *st[4]=" STR "; It declares a pointer to a constant string, but since the memory space is a constant, the contents of the address cannot be changed, for example: *st="no"; // this is not possible because the contents of the memory space cannot be modified st = "ok"; // this is ok, although the contents of the memory can not be modified, but the pointer can be modified.
Pointer constant refers to a pointer to a fixed memory unit. The contents can be modified, but the location cannot be changed. For example: char *const st[4]=" STR "; * st = "ok"; // this is ok because the content pointing to the location can be modified. St = "no"; // this does not work because the pointer cannot change the pointing position, that is, if it is initialized to address 0X8000, it can only point to this position and cannot be changed, but the contents of 0X8000 can be changed.

Char * s = "abcde".
S [0] = '1'; // there is an error in this sentence at runtime

The following is ok:

Char [] s = "abcde";
S [0] = '1';


Because:

String constants cannot be changed and are compiled into read-only memory (static storage).

Char [] and char* can be used in many ways, but the tricky thing is that the header pointer is the same.

Because the "behavior of modifying string constants by pointer" is undefined, someone last asked "why the above code does not report errors under TC but under VC6.0". However, most implementations now set the storage space of the constant string to readonly, so run the error;

Char * s = "abcde". Const char *s = "abcde";

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
According to you here, the compiler should not declare:
Char * s = "abcde".
And force us to declare as follows:
Const char *s = "abcde";
Why didn't the compiler do that?
--------------------------------------------
Yes, the compiler should prohibit this behavior and help the programmer write the right program, as the developer said.
But this is a special case, a special case!
Why do you say that? Because char *s = "abcde"; This pattern has been used by many people (including building owners), and it is so widely used that the standard gives it some latitude: it allows such code to be compiled.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Effective c++ note 1 on page 91:
In C++ standard, "Hello" is typed as const char[]; This type is almost always treated as const char*. So we expect that taking a string literal, such as "hello", as the initial value of a char* variable would violate the constants. But this behavior is so frequent in C that the C++ standard specifically exempts initialization actions like this. However, you should avoid doing this because it is not accepted.


Related articles: