C ++ array initialization and definition, one-dimensional array and two-dimensional array, two-dimensional array of dimensions

Source: Internet
Author: User

C ++ array initialization and definition, one-dimensional array and two-dimensional array, two-dimensional array of dimensions
There are two ways to input and output character Arrays:
1) one character input and output, for example, 5.9.

2) input or output the entire string once. For example, there are the following program segments:
Char str [20];
Cin> str; // enter a string using the character array name
Cout <str; // output string with character array name
Enter a string at runtime, for example:
China
In the memory, the status of the array str is 5.9, and an ending sign '\ 0' is automatically added after the five characters ′.

How do I define one-dimensional arrays and two-dimensional arrays in C?

One-dimensional array: type specifier array name [constant expression]
For example, int a [10] indicates that the array name is a. the array has 10 elements.
Two-dimensional array: array name [subscript] [subscript]
For example, a [2] [3] subscript can be an integer expression. For example, a [2-1] [2*2-1] cannot be written as a [2, 3].
A [2-1, 2*2-1]

C language for the definition of two-dimensional arrays

First, you need to understand the true meaning of a two-dimensional array. The first dimension is the starting address of a group of data, and the second dimension is a value in a group of data. secondly, you need to know what a [] [4] means that each dimension of a's two-dimensional array is a one-dimensional array composed of four elements. the compiler can calculate the length of the first dimension based on the number of initial values assigned to a (total number of elements/4). If division is not allowed, the length is increased by 1, there should also be several array elements without initial values ). the meaning of a [3] [] can be established on the surface, but the problem is that the compiler cannot directly identify the exact length of the second dimension (you can consider, if the total number of initial values is 2, the second-dimensional length of a can be considered as 1 or 2 ,.......), this will produce errors. therefore, a [] [4] = {*,*,*......} Instead of defining a [3] [] = {*,*,*......}.
I have already discussed your question. because the initial value is five elements, 5/4 = 1, but not all, so we need to add 1, that is, the actual array a is essentially defined as a [2] [4], the element is:
1 2 3 4
5 0 0 0 (the three following 0 are default values)
Hope to help you. ^-^

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.