Two tips on C-language pointers sharing _c language

Source: Internet
Author: User

1: constant pointer and pointer constants

constant pointers are very close to the name of a pointer constant, but they differ greatly.
A constant pointer is a pointer to a constant, for example, Char const *st[4]= "str", or, using the following method, the effect is the same: const char *st[4]= "STR"; It declares a pointer variable, which points to a constant string, but because the memory point is constant, the contents of the address cannot be modified, for example: *st= "no"; This is not possible, because the contents of the memory space can not be modified st= "OK"; This is possible, although the contents of the pointed memory cannot be modified, but the pointer's point can be modified.
A pointer constant is a pointer to a fixed memory unit whose contents can be modified but cannot be changed to the point where it is pointed, for example: Char *const st[4]= "str"; *st= "OK"; This is possible because the content pointing to the location can be modified. St= "No"; This does not work because the pointer is not able to change point position, that is, if initialized to address 0x8000, then it can only point to this location, can not change, but 0x8000 content can be changed.

Char *s = "ABCDE";
S[0] = ' 1 '///This sentence has an error at run time

The following are possible:

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

  
Because:

String constants are not modified and are compiled into read-only memory (static storage);

Char[] and char* can be common in many places, but there is a subtle relationship between the head pointer and the same;

Because "the behavior of modifying string constants through pointers" is undefined, the last person asked "why the above code does not complain under the TC and VC6.0 under the error." However, now most of the implementation of the constant string of storage space set to Read-only, so the run times is wrong;

Char *s = "ABCDE"; equivalent to const char *s = "ABCDE";

-------------------------------------------
According to everyone upstairs, the compiler should prohibit this statement:
Char *s = "ABCDE";
And force us to declare as follows:
const char *s = "ABCDE";
Why didn't the compiler do that?
--------------------------------------------
Yes, indeed, as the landlord said, the compiler should prohibit this behavior, and thus help programmers write the correct program.
However, this is a special case, is an exception!
Why do you say that? Because char *s = "ABCDE"; this pattern has been used by many people (also including the landlord), it is used so widely that the standard gives it some tolerance: allow such code to compile.

-------------------------------------------- "Effective C + +" 91 page Note 1:
In C + + standard, the "Hello" type is a const char[]; This type is almost always considered a const char*. Therefore, we expect that the initial value of a char* variable as a string literal constant (string, for example, "Hello") would violate the constants. But this kind of behavior in C language is too frequent, so C + + standard for such initialization action special grant and exemption. However, you should avoid doing so, because it is not accepted by everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.