C + + arrays and pointers

Source: Internet
Author: User
Tags array definition

1: Array

An array is a composite data type consisting of a type name, an identifier, and a dimension that specifies the type of the element that is stored in the array, and the number of elements in the array that is specified by the dimension.

The type name in an array definition can be a built-in data type or a class type, and the type of an array element can be any compound type, except for references. None of the elements are reference arrays .

The dimensions of an array must be defined with a constant expression with a value greater than or equal to 1

Const int 3;   
Const unsigned SZ = get_size () error, run-time only know the size of no
// int a = 3; Error int B[a];

when initializing a character array with a set of character literals, be sure to remember to add the empty character of the end string . For example, the following initialization will cause a compile-time error:

Const Char ch3[6"Daniel"//

Unlike vectors, an array cannot be initialized with another array , nor can an array be assigned to another array, which are illegal

The following table type of vector is vector::size_type the subscript type of the different array is size_t;

2: pointer

int Main () {    int5;     int 0 // p initialized to address no object output to 00000000    int *p1 = &v;     "        "" "<< p1 << Endl;}

the downside of non-initialization : If you use an uninitialized pointer, the indeterminate value stored in the pointer is treated as an address, and the bit content stored in that memory address is manipulated. Using an uninitialized pointer is equivalent to manipulating the underlying data stored in this indeterminate address. Therefore, when you dereference an uninitialized pointer, it usually causes the program to crash

assigning an int variable to a pointer is illegal, although the value of this int variable may be 0. However, a const value of 0 or 0 can be obtained at compile time to assign the pointer:

intival; intZero =0; Const intC_ival =0; int*pi = ival;//error:pi initialized from int value of Ivalpi = zero;//Error:pi assigned int value of zeroPI = c_ival;//Ok:c_ival is a const with compile-time value of 0PI =0;//ok:directly initialize to literal constant 0

You can only use the following four types of values to initialize or assign a pointer :

1.0-Value constant expression (section 2.7), for example, an integer const object or literal constant 0 that can be obtained at compile time for a 0 value.

2. The address of the type-matching object.

3. The next address at the end of another object.

4. Another valid pointer of the same type.

assigning an int variable to a pointer is illegal, although the value of this int variable may be 0. However, a const value of 0 or 0 can be obtained at compile time to assign the pointer:

The pointer can modify the value of a variable, and a pointer to a const value is also qualified as a pointer to a const object , otherwise the pointer modification value changes so that the const value is meaningless. const double *P;

In the actual program, pointers to const are commonly used as formal parameters for functions. Defines a parameter as a pointer to a const to ensure that the actual object passed to the function is not modified in the function because of the parameter.

the difference between a const pointer and a pointer to a const object http://blog.csdn.net/tobacco5648/article/details/8530975

Dynamic arrays

Each program occupies a usable memory space at execution time, which is used to hold dynamically allocated objects, which are called free stores or heaps of programs. The C language program uses a pair of standard library functions, malloc and free, to allocate storage space in a discretionary store, while the C + + language uses the new and delete expressions to achieve the same functionality.

Array variables are defined by specifying the type, array name, and number of dimensions. When allocating arrays dynamically, you only need to specify the type and array length, do not have to name the array object, and the new expression returns a pointer to the first element of the newly allocated array

int New int [Ten//

This new expression assigns an array containing 10 elements of type int and returns a pointer to the first element of the array, which initializes the pointer Pia.

you can use a pair of empty parentheses following the length of the array, initialize the value of an array element, and for dynamically allocated arrays, its elements can only be initialized to the default value of the element type, and not as an array variable, providing different initial values with the initialization list for the array element.

int New int [Ten//

C + + arrays and pointers

Related Article

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.