Pointer variable pointing to a Common Object

Source: Internet
Author: User

To better understand the concept and use of pointer variables pointing to a common object, first understand the pointer variables pointing to a constant variable, and then further study the pointer variables pointing to a common object.

The following defines a pointer variable pointing to a constant variable.

PTR: const char * PTR;

Note that the position of const is at the leftmost. It is closely connected to the char type name, indicating that the char variable pointed to by the pointer variable PTR is a constant variable and cannot be changed through PTR.

The general form of a pointer variable that defines a pointer to a constant variable is

Const type name * pointer variable name;

Note:

(1) If a variable has been declared as a constant variable, it can only be pointed to by a pointer variable pointing to a constant variable, rather than a general variable (pointing to a non-const type variable) the pointer variable points to it.

(2) A pointer to a constant variable can point to a variable that is not declared as a const, in addition to a constant variable. In this case, you cannot use this pointer variable to change the value of this variable. If you want to change the value of C1 in no case, you should define it as a const type.

(3) If the form parameter of a function is a pointer to a non-const variable, the real parameter can only be a pointer to a non-const variable, rather than a pointer to a const variable, during function execution, you can change the value of the variable (that is, the variable pointed to by the real parameter pointer) pointed to by the form parameter pointer variable.

If the form parameter of a function is a pointer to a const-type variable, it is obvious that the value of the variable pointed to by the pointer variable cannot be changed during function execution. Therefore, the real parameter is allowed to be a pointer to the const variable, or a pointer to a non-const variable.

The above correspondence is consistent with the pointer variable described in (2) and the variable to which it points: pointer variables pointing to constant variables can point to const and non-const variables, while pointer variables pointing to non-const variables can only point to non-const variables.

The above is a pointer variable pointing to a constant variable. The concept and usage of a pointer variable pointing to a constant object are similar to this. You only need to replace "variable" with "object.

(1) If an object has been declared as a constant object, it can only be pointed to by a pointer variable pointing to a constant object, rather than a general object (pointing to a non-const object) the pointer variable points to it.

(2) If you define a pointer variable pointing to a common object and point it to a non-const object, the object to which it points cannot be changed through a pointer. If you want to change the value of T1 under any circumstances, you should define it as a const type.

(3) pointers to common objects are most commonly used in function parameters, so that they are not modified during function execution.

Remember this rule: if you do not want to modify the object value when calling a function, you should define the form parameter as a pointer variable pointing to a common object, use the object address as the real parameter (the object can be of the const or non-const type ). If the object is required not only to be changed during function calling, but also to be unchanged during program execution, it should be defined as Const.

(4) If a pointer variable pointing to a common object is defined, it cannot be used to change the value of the pointed object, but the value of the pointer variable itself can be changed.

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.