Arrays and pointers
- Array vs vector: The array length is fixed and the array length is not known; arrays cannot be copied directly or assigned vectors can be
- Character array particularity: Char chr1[3]={' C ', ' + ', ' + '};char chr2[4]= "C + +"; the latter will put a null character in the last way, representing the end of the character array, requiring a number of characters + 1 lengths
- Array Subscript Index: Subscript type is size_t (unsigned integer large enough) pointer index: vector-like iterator index
- Should try to avoid using arrays and pointers error-prone C + + should use vector iterator string, etc. instead
- pointer: string *str; string* str; both, usually using the former, string *str1, *STR2 (both pointers); srting* Str1,str2 (people mistakenly think that both pointers are actually only str1 Yes );
- Pointers: Be sure to initialize, when pointing to the object has not been established can be initially 0/null, NULL is inherited from C language, is a preprocessing variable, compile-time processing as 0,null is not the STD space does not need std::null
- void * Pointer: a particular can hold the address of any type of object, but cannot be used to manipulate the object it points to
- Pointer reference differences: references must be initialized and point to only one object assignment operation has different meanings
- Using an array name in an expression automatically translates to a pointer to the first element of the array
- Pointer subtraction to get data type ptrdiff_t (signed integer is large enough)
- A const pointer to a const object pointing to a const pointer to a const object
Const int*a;//the object is a const pointer that can point to another but cannot modify the value of the object by a pointerint*ConstA//pointer to a const pointer to a value that cannot be modified to point to an object can be modifiedConst int*ConstA//const pointer to const objectConst intI=1;int*p=&i;//Error cannot assign the address of a const object to a normal pointer to a non-const objectvoid*p=&i;//ErrorConst int*p=&i;//OKConst void*p=&i;//OK
String *pstring; const Pstring CStr; // CStr is a string *const type the following three kinds of expressions are the same meaning
String *const sctr;
pstring Const SCTR;
- Dynamic arrays: Dynamic arrays are determined at run time. The array-length heap space is used to store dynamically allocated objects The new Delete implementation requires the Delete to free up space before the string *str=new String[n] ();d elete [] str;
- compatibility with old and new code: all places using string literals in strings You can use C-style strings instead of vice versa; use arrays to initialize vectors;
char *chr=str; // error const char *chr=str.c_str (); // Returns a const pointer to prevent the CHR pointer from modifying the STR object int arr[]={0 , 1 , 2 , 3 , 4 , 5 };vector <int > Ivec (arr,arr+< Span style= "color: #800080;" >6 ); // Pointer to the first element and the last element in an array-initialized vector
-
int ia[3[4]; int (*IP) [4]=ia; // IP pointer to int[4] A pointer to an array of 4 elements can ip+2 up to ip=&ia[2]; int *ip[4]; // array array of length four element type is int* int int_array[4]; typedef simplifies pointer *ip=ia of multidimensional arrays ;
C-style string
- C-style strings are supported by C + + but should not be easy to bring a lot of security issues in C + +
- The type of string literal is an array of const char types
- C-style strings are not C-type or C + + types are character arrays that end with null characters char chr[]={' C ', ' + ', ' + ', '};char ' chr[]= "C + +", char *chr= "C + +", all C-style strings. This is not true if NULL is not added. (const) char* to manipulate C-style strings standard library CString is a standard library of C + + that handles style characters (same as String library content in String.h C)
- The array needs to tell its length otherwise it cannot get its own length; character arrays do not know the length of the array without the null character, but C-style strings can be traversed by a null character to get the length so C-style string is a corresponding function can be called; note that input S is a string that is not 0 empty characters and the target string needs There is enough space; the function at the beginning of the strn is safer than the strcpy strcat and can control the number of characters copied
Strlen (s); // return length does not contain null characters strcmp (S1,S2); // equal returns 0 S1>S2 returns a positive number otherwise returns a negative number strcat (S1,S2); // stitching to S1 and returning S1strcpy (S1,S2); // Copy to S1strncat (s1,s2,n); // The first n characters of S2 are spliced to S1strncpy (s1,s2,n); // copies the first n characters of a s2 to S1
C + + Primer (No. 456 chapter)