Basic course of C language (ii) data types, variables, and operators (8)

Source: Internet
Author: User
4.4 Array Variables
An array is defined as a set of variables with the same data type and has a common name. Each specific element in the array is accessed using subscript. The array is composed of a contiguous storage address, the lowest address corresponds to the first array element, and the highest address corresponds to the last array element. An array can be one-dimensional or multidimensional. The Turbo C2.0 uses array variables as well as its advanced language side.
One or one-D array
The description format for a one-dimensional array is:
Type variable name [length];
A type is a data type, that is, the data type of each array element, including integer, floating-point, character, pointer, and struct and union.
For example:
int a[10];
unsigned long a[20];
Char *s[5];
Char *f[];
Description
1. Arrays are labeled with 0 as the first element, so when an integral array of int a[16] is described, it indicates that the array has 16 elements, a[0]~a[15], and one element is an integer variable.
2. Most strings are represented by one-dimensional arrays. The number of array elements represents the string length, and the array name represents the address of the first character in the string, for example, after the "hello" string is stored in the array described in statement char str[8], and STR represents the memory cell address of the first letter "H". Str[0] Stores the ASCII value of the letter "H", and so on, str[4] deposits the ASCII value of the letter "O", and str[5] should hold the string Terminator ' ".
3. Turbo C2.0 are not checked for bounds. For example, use the following statement to illustrate two arrays
Char str1[5], str2[6];
When assigned to str1 a string "ABCDEFG", only "ABCDE" is assigned, "E" will be automatically assigned to STR2, which should be given special attention.
Three, multidimensional array
The general description format for multidimensional arrays is:
Type array name [nth dimension length] [n-1 dimension length] ... [1th dimension length];
This explanation is different from the description of multidimensional arrays in basic, Fortran and other languages.
For example:
int m[3][2]; /* Defines a two-dimensional array of integers */
Char c[2][2][3]; /* Defines a three-dimensional array of character types * *
Array M[3][2] A total of 3*2=6 elements, in the order of:
M[0][0], m[0][1], m[1][0], m[1][1], m[2][0], m[2][1];
Array C[2][2][3] A total of 2*2*3=12 elements, in the order of:
C[0][0][0], c[0][0][1], c[0][0][2],
C[0][1][0], c[0][1][1], c[0][1][2],
C[1][0][0], c[1][0][1], c[1][0][2],
C[1][1][0], c[1][1][1], c[1][1][2],
The memory space (that is, the number of bytes) occupied by the array is calculated as:
Number of bytes = 1th Dimension length * 2nd dimension length *...* N dimension Length * The number of bytes occupied by the array data type.

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.