Array and pointer

Source: Internet
Author: User

 


Type name, IdentifierAnd DimensionComposite data type. The type name specifies the element type stored in the array, and the dimension specifies the number of elements contained in the array. Constant expression definition with a value greater than or equal to 1. This constant expression can only contain an integer literal value constant, an enumerated constant, or an integer const object initialized using a constant expression. Non-const variables and const variables whose values are not known at the running stage cannot be used to define the dimension of the array.
 

 

.
  • The built-in array defined in the function body has no initialization and its value is uncertain;
  • No matter where the array is defined, if its element is of the class type, the default constructor of the class is automatically called for initialization. If the class does not have the default constructor, the array elements must be explicitly initialized.

3. Special Character Array

Character Arrays can be initialized with a set of character denominations enclosed by curly brackets and separated by commas (,) or a string nominal value.

The string literal value contains an additional null character used to end the string.

When a new array is initialized using the string literal value, an empty character is added to the new array:

  ca1[] = { ,  , };         ca2[] = { ,  ,  , };       ca3[] = ;   

The dimension of cache is 3, while that of ca 2 and Ca 3 is4. When initializing character arrays with a group of character denominations, remember to add the empty character of the ending string.

Size_t.

 

      size_t array_size =       (size_t ix=; ix != array_size; ++          ia[ix] = ix;  

 

Buffer overflow errorThis error may occur when the subscript references an array or other similar data structure boundary.

 

 


  • Constant expression with 0 values
  • Address of the Type-matched object
  • Next address after another object
  • Another valid pointer of the same type

5. pointers of the void * type can save the addresses of any type of objects.

Pointer operation

[Cpp]View plaincopyprint?
  1. Int ivals = 1023;
  2. Int * pi = & ival;
  3. Int ** ppi = π
[Cpp]View plaincopyprint?
  1. Int ia [] = {0, 2, 4, 6, 8 };
  2. Int I = ia [0];
[Cpp]View plaincopyprint?
  1. Int * p = & ia [2]; // OK: p points to the element indexed by 2
  2. Int j = p [1]; // p [1] = ia [3], OK: p [1] equivalent to * (p + 1 ), p [1] is the same element as ia [3]
  3. Int k = p [-2]; // p [-2] = ia [0] OK: p [-2] is the same element as ia [0]
Although the subscript is-2, it is parsed as an offset by the compiler, so it is equivalent to * (p-2 ).10. The result of the two pointer subtraction operations is the ptrdiff_t data of the standard library type. Like the size_t type, ptrdiff_t is also a machine-related type, which is defined in the cstddef header file. Size_t is of the unsigned type, while ptrdiff_t is of the signed integer type.
ptrdiff_t  n = ip2 - ip; 

11. C string standard library functions

Ca [] ={,}; cout <strlen (ca) <endl;

In this example, if ca is an array of characters without a null Terminator, the calculation result is unpredictable.

Strlen returns the total number of characters in the space. In any case, this value cannot be correct.

    (size_t ix =  ; ix != arr_size ; ++     cin >>  vector<> ivec(int_arr , int_arr + arr_size);

 


[Cpp]View plaincopyprint?
  1. Int * pia = new int [10]; // array of 10 uninitialized ints
[Cpp]View plaincopyprint?
  1. String * psa = new string [10]; // defines 10 empty string arrays.
  2. Int * pia = new [10]; // defines 10 uninitialized int arrays.
[Cpp]View plaincopyprint?
  1. Delete [] pia; // This statement recycles the array pointed to by pia and returns the printed memory to the free storage zone.

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.