C + + Self-study notes _ Arrays and pointers _ C + + Primer

Source: Internet
Author: User

  

 1. the type in the data definition can be either a built-in data type or a class type, except for the reference data type , the type of the array element is any data type. Not all elements are arrays of reference data types.

  2. The dimensions of the array must be defined with an expression with a value greater than or equal to 1. This constant expression can only contain literal literals constants , enumeration constants , or const objects initialized with constant expressions, otherwise the dimensions of the array are not known at compile time, and for a non-const object, only the runtime to get its value.

Const unsigned buf_size=; int staff_size=; Char input_buffer[buf_size];    // ok:const Variable double salaries[staff_size];    // Error:non Const Variable

  3. about Initialization

If the provided element initial value is not displayed, the array is initialized like a normal variable:

  ·  A built-in array defined outside the function body , whose elements are initialized to 0;

  ·  A built-in array defined within the function body , whose elements are not initialized ;

  ·  The default constructor for the class is automatically initialized, regardless of where the array is defined, if its element is a class type ;

 string  sa[10 ]; //  element initialized to an empty string               int  ia[10 ]; //  element initialized to 0  int   Main () { string  sa2[10 ]; //     element initialized to an empty string         int  ia2[10 ]; //     no initialization  return  0       

  4. about void* pointers

C + + provides a special pointer void*, which can be used to hold the address of any data type object.

double obj=3.14; double *pd=&obj; void *pv=&obj;  // ok:void* can hold the address value of any data pointer typePV=PD;

The void* pointer supports only a few limited operations: compare to another pointer, pass a void* pointer to a function, or return a void* pointer from a function, and assign a value to another void* pointer.

  Note: You are not allowed to manipulate the object that it points to by using the void* pointer.

5. the phrase "the reference cannot point to another object once it is initialized" is not fully understood before. In fact, it's like this: C + + stipulates that a reference must be initialized immediately, and when I assign a value to a reference once again (I thought it was a reference to another object ...), the value of the object associated with the reference is actually modified by the reference assignment, rather than associating the reference with another object (This is why the reference must be initialized when it is defined).

  6. Pointers to const objects

Const double *cptr;

The cptr here is a pointer to a double const object, meaning that the cptr itself is not a const and does not need to be initialized when it is defined. If necessary, you can also reassign the cptr to another const object (note here that the pointer to a const object thinks it is a pointer to a const object, so when you assign a value to Cptr again, If the object that the Cptr points to is no longer a const type, it is not allowed .

  Assigning a const object's address to a normal, non-const pointer can also cause compile-time errors. (The so-called pointer to a const object also has a const attribute).

Const double pi=3.14; Double *ptr=π         // Error Const Double *cptr=π  // OK

  Allows the address of a non-const object to be assigned to a pointer to a const object.

Const Double *cptr; double dval=3.14; cptr=&dval;       // ok:but can ' t change dval through cptr  

Cptr once defined, it is not possible to change the value it points to by Cptr, but it does not mean that the non-const object that the CPTR points to cannot be modified and can be modified by ordinary pointers to non-const objects.

  7.const pointer

int errnum=0; INT *const curerr=&errnum;  // Curerr is a constant pointer

means that the Curerr pointer can no longer point to another object once it is initialized. An attempt to assign a value to any const pointer causes a compile-time error. However, the fact that the pointer ability is const does not indicate whether the value of the object it points to can be modified by a const pointer, and whether the modification depends entirely on the type of object it refers to.

  8. const pointer to const object

Const double pi=3.14; Const Double Const *pi_ptr=π

So understand: First, pi_ptr is a const pointer, and it points to a const object. Result: Neither modifies the value of the object pointed to by pi_ptr or modifies the point of the pi_ptr.

  9. Creating a dynamic array

Dynamic arrays do not mean that the size of an array can vary, but rather that it is not necessary to know the length of the array at compile time, and to determine the length of the array at run time. Also, unlike an array variable, a dynamically allocated array will persist until the program explicitly releases it.
  Heap : Each program consumes a piece of available memory space at execution time, which is used to hold dynamically allocated objects , known as the program's heap. The C language uses a pair of standard library functions malloc and free allocate storage space in the heap, and the C + + language uses the new and delete expressions to implement the same functionality.

9.1 Definition of dynamic arrays

When allocating an array dynamically, you only need to specify the type and array length, and you do not need to name it, and the new expression returns a pointer to the first element of the allocated memory space.

int *pia=newint[ten];

9.2 Initializing an array of dynamically allocated

When an array is dynamically allocated, the default constructor of the calling class is initialized if the array element class type, and if the array element is a built-in data type, it is not initialized (as distinguished from the initialization of the above function and the array variables outside the function).

string *psa=newstring[ten];  // Initialize all to empty string int *pia=newint[ten];        // Do not initialize

  

C + + Self-study notes _ Arrays and pointers _ C + + Primer

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.