C + + Primer essay Chapter 4 arrays and pointers

Source: Internet
Author: User

1. Array : An array is a conforming data type consisting of a type name, an identifier, and a dimension, which specifies the type of element that is stored in the array, and the dimension specifies the number of elements in the array and the identifier is the name of the array. For example:

int arr[10]; where int is the type name, arr is the identifier and 10 is the dimension of the array.

2. Array definition and initialization: the initialization of the array should pay attention to the following points

(1). The element type stored in the array cannot be a reference type, except that it can be any other type.

(2). The dimension of an array must be a literal constant, an enumeration constant, or a constant expression (but not at runtime to know its worth constant expression)

(4). When the initialize array is displayed, if the number of elements within {} is less than the number of array dimensions, the remaining elements are initialized to 0, and if the number of elements within {} is greater than the number of dimensions of the array, an error is presented.

(5). If the initialization array is not displayed, the array is initialized like a normal variable:

(a). The built-in array defined outside the function body, whose elements are initialized to 0.

(b). The built-in array defined within the function body has no initialization of its elements.

(c). Regardless of where the array is defined, if its element is a class type, the default constructor for that class is automatically called to initialize. If the class does not have a default constructor, it must provide display initialization for the elements of the array.

3. Pointer: The pointer is used to point to the object. Specifically, the pointer holds the address of another object.

4. Definition of the pointer:

(1). When the pointer is defined, the recommended notation should be the int *ip instead of the int* IP, which means that the * is closer to the variable. Why is that? If a definition so writes int* IP1,IP2, it will make many people think that int * is a pointer type, so that ip1 and IP2 are defined as int * type, actually only ip1 is and ip2 is int type. But if you write this: int *ip1,ip2, you won't make such a big mistake.

(2). The initialization or copying of pointers can only be one of four types of values:

(a). 0 value expressions, for example, 0 worth of Integer const objects or literal constants 0 are obtained at compile-time.

(b). The address of the type-matching object.

(c). The next address after another object.

(c). Another valid pointer of the same type.

5.void pointer : It can hold the address of any type of object.

6. Comparison of pointers to references:

(1). A reference always points to an object: An error is not initialized when the reference is defined.

(2). Assigning a value to a pointer is to have the pointer point to another object and assign a value to the reference to modify the value of the object that the reference is associated with.

7. Pointers and const qualifiers:

  (1). Pointer to const object: const int *CIP; The pointer defined here, CIP, is a pointer to a const object, indicating that the contents of the CIP point cannot be changed by the dereference operation of the pointer.  

(2). Const pointer: int errnumb = 0; int * Const CURERR = &errNum; The Curerr defined here is a const pointer, which means that the pointer itself cannot be changed, so it must be initialized when it is defined.

(3). A const pointer to a const object: const int PI = 3; const INT * Const PI_PTR = π This defines a const pointer to a const object that cannot be changed by itself and the object to which it is pointing.

(4). It is important to note that:

(a). You cannot use the void * Pointer to save the address of a const object, but you must save the address of the const object with a pointer of the const void * type, which is an incorrect example:

const int universe = 42; void * CPV = &univers; The const void * CPV = &universe is the correct notation.

(b). The address of a const object cannot be assigned to a pointer that is not a const object, but it allows the address of a non-const object to be assigned to a pointer to a const object. Examples are as follows:

Const double PI = 3.14;//↓| Double pi = 3.14; ↓

Double *ptr = π   Fatal error ↓| Const double *CPTR = π//correct

Const double CPTR = π That's right

8. C-style string : char p[] = "Hello"; The P-Dimension of this array is 6 instead of 5, why? Because there is also a null character after the string, null. A similar character array that ends with null characters is a C-style string. We should note that the library function of a series of C-style strings provided by the C standard library requires that the passed-in string argument must end with null characters.

9. Pointer array with array pointers:

  (1). int (*p) [10]; Due to the high priority of (), p is a pointer to the *, which means that P is an array pointer, and a pointer to an array of 10 shaping elements.

(2). int * P[10]; Because there is no (), the [] priority is high, and P is first combined into an array, indicating that the array is represented by P. and int * In this case you can look at a data type---an integer pointer. This is easy to understand: The array P contains 10 elements of the int * type.

Recommendation: Avoid using pointers and arrays as much as possible, use vectors instead of array, use iterators instead of pointers, and replace C-style strings with string types.

C + + Primer essay Chapter 4 arrays and pointers

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.