How to distinguish "Pointer to Const Object" and "Const Penso"?

xiaoxiao2021-03-06  123

Const is a new keyword introduced in C , which brings great convenience for C programming. Pointer and Const pointers pointing to the Const object are the concepts close to the two names, which is very easy to confuse for beginners, and distinguishes them here.

Pointer to the Const object

You can understand this pointer to the Const object:

Pointer to the Const object is a pointer, can't pass it to modify the value of the object it pointing.

· Declaration method: const INT * P;

The Const object is not allowed to modify its value after initialization, so we cannot use a normal pointer to a const object, that is, the following assignment will cause compilation errors:

Const I = 1;

INT * P = & I;

Otherwise, we can use the normal pointer to modify the value of a const object, then Const is meaningless.

The correct way is to use a pointer to the Const object to get the address of the Const object:

Const I = 1;

Const Int * P = & I;

In this way, the value of the Const object that is not possible to modify the pointer to the Const object is also used.

Need to pay attention to the two points:

Pointer to the Const object itself is not a const type (this is also the main difference between it and the const pointer), so it can point to another const object pointing to the CONST object to be given an address of a non-Const object, but at this time at this time at this time This pointer to modify the value of the object is illegal.

2. Const pointer

Can understand the Const pointer this:

The Const pointer is a pointer. It itself is a const type, so after initializing it, it cannot change its point to point, that is, can not let it point to a new object.

Disclaimer:

INT * const p; // Pointer to a const pointer to non-Const objects

Const int * constp; / / point to Const pointers to Const objects

It can be seen from the above declaration method that the Const pointer can point to the Const object and a non-Const object, but the declaration method of both is different.

With the Const pointer, you can not modify its address value, but the Const pointer points to a non-Const object, you can use it to modify the value of the object it pointing to.

to sum up:

The reason why beginners will confuse the root cause of the two is some implicit assumptions in their minds, that is, say that they want to give the object to be analyzed to analyze the properties that they don't exist, this is a lot of people in processing. A common problem that is easily made when the problem is. As long as the grammar rules are not clear, we can use it, but there is no need to add all kinds of restrictions. In fact, we must figure out the difference between the two, as long as the two points are enough:

The pointer itself is a Const type or a non-const type pointer to the object to be a const type or a non-Const type

The value of the CONST type variable is not allowed to change (this is the foundation) after initialization, then the Const pointer does not change, the Const object is not changed, everything is clear.

2. To figure out the two questions above, there is a very simple way:

If the pointer is next to the keyword for const, it is a const pointer; if there is a const key before the object type points to the pointer, then it is a pointer to the COSNT object.

Apply the above judgment method, const * const p; indicates the const pointer to the Const object INT.

转载请注明原文地址:https://www.9cbs.com/read-126211.html

New Post(0)