C ++ data type-pointer, String, const, reference

Source: Internet
Author: User

The pointer should pay attention to the corresponding type, and do not perform operations similar to int * to int.
Int ** p = 0; // pointer to the pointer, so that it cannot point anywhere
Int * P1 = * P;

Int array [10];
Int * P1 = array; // array name specifies the first address of the array
Int * P1 = & array [0]; // point to the address corresponding to the first element

Common Operations on strings:
Traversal string

Int string_length (const char * st)
{
Int CNT = 0;
If (ST) // determines whether st points to an object
While (* st ++) // first execute st ++ and then obtain its value, that is, to determine whether each character of the string is 0 (the end is 0)
++ CNT;
Retrun CNT;
}

Reference string
Const char * ST = "aaaaaaaaaaaa ";
Const char * P = sT; // Add a pointer here to operate P and keep the first pointer of St.
While (* P ++)
...

Const

Int I = 8;
Const int * A = & I; // pointer to a constant integer. the pointer to the address is variable, but the value is variable. (OK: A = & I2; error: * A = 9 ;)
Int * const A = & I; // a constant pointer to the int * type. The pointer to the address is immutable, but the value is variable. (OK: * A = 9; error: A = & I2 ;)
Const int * const A = & I // the address and value cannot be changed.

For example:
Const int IC; // error, not initialized
Const int * IC; // correct. It is a pointer (only pointing to the const int type) and does not need to be initialized.
Int * const IC; // error. It is not initialized and is a constant (type: int *). Therefore, Initialization is required.

Int I = 8;
Const int Ic = I; // OK
Const int * PIC = & I; // OK (const int can point to int or const INT)
Const int * PIC = & IC; // OK
Int * const PIC = & I; // OK (int * can only point to int *, cannot point to const int *)
Int * const PIC = & IC; // Error
Const int * const pic2 = & IC; // OK

Reference

Int * Pi = NULL;
Int * & rval = PI; // Pointer Reference

Regular reference
* It can point to unaddressable values, such as constants.
* Pointing to different types (as long as one type can be switched to another type)
Double dval = 2.88;
Const Int & ir= 1024;
Const Int & ir2 = dval; // The Compiler first converts dval to int, and then points the reference to it.
Const Int & ir3 = dval + 1.0;

Const int ival = 1024;
Const int * const & pi_ref = & ival (? I still don't quite understand it)

Boolean
Boolean values cannot be declared as signed, unsigned, short, long
For example, the following statement is incorrect:
Short bool found = false;

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.