C Language Learning-array and language learning Array

Source: Internet
Author: User

C Language Learning-array and language learning Array

Array

What is an array?

Arrays can store multiple identical data types.

Why use arrays?

Programs often use data of the same type. For example, to process the Student Score information of a class, if there are only a few students, we can use several variables of the same type, such:

Int mark0, mark1, mark2, mark3, mark4;

In this way, the scores of five students can be stored, but how many people are there? Do you want to keep writing like this? If you think there is nothing wrong with continuing writing, there may be thousands or even tens of thousands of people. Therefore, how to Reasonably organize a large number of similar data is a problem.

Reasonable organization includes:

(1) Allocate storage space for each data.

(2) Each data should have a unique identifier for reading, writing, and searching.

In this application background, the Array application was born and successfully solved the above problem.

When declaring an array, the compiler allocates memory storage space for the array. It is worth noting that the memory space occupied by the array is continuous. In this way, it is easy to calculate the memory size occupied by the array and the first address of the memory corresponding to each element. For example, for an array with a size of N and a type of short, the memory size occupied by the array is:

N * sizeof (short) = N * 2

If the address of the 1st elements in the memory is p, the address of the M element (M not greater than N) in the memory can be expressed:

(M-1) * sizeof (short)

You can only reference the elements of each row and column individually.

One-dimensional array:

A one-dimensional array is also called a vector. It is used to organize a group of data of the same type with a one-dimensional sequence. Before using an array, the compiler must declare the array and allocate memory to it based on the declaration statement, this makes the array meaningful.

To open up a piece of continuous memory in the memory for Array use, you need to consider the following questions: first, where to open up, but where to open up, in C language, this is automatically done by the compiler, programmers say that they need to "raise requirements", that is, the number of elements that should be stored in the opened array, and the type of each element. In addition, programmers need to specify the array name.

The basic format of one-dimensional array declaration is:

Type array name [number of array elements];

For example, the declaration statement:

Double sz [6];

It tells the compiler three pieces of information: the array name is sz, the elements in the array are double type, and the number of elements in the array is 6. In this way, you can read and write the array and array elements.

To prevent subscript out-of-bounds errors, for the array sz declared above, the valid subscript is 0 to 5. If sz [6] appears in the program, sometimes the compiler does not report errors, but this may cause program crashes.

 

Two-dimensional array:

Two-dimensional arrays can also be used to initialize elements in the initialization table during declaration. For example:

Int num [2] [3] = {1, 2, 3}, {4, 5, 6 }};

Two-dimensional arrays can also initialize some elements, such:

Int num [2] [3] = {1}, {2 }};

Note: The inner curly braces in the initialization expression represent a row. In this way, the initialization of the first several elements is different from that of the one-dimensional array. Some intermediate elements can be skipped during the initialization of the Two-dimensional array, assign values to the following elements. After learning about the two-dimensional array memory distribution, you may have a deeper understanding.

The preceding statement declares an array of two rows and three columns. Only the 1st elements in each row are initialized. Other elements are initialized to 0 by default, which is equivalent:

Int num [2] [3] = {1, 0}, {2, 0 }};

The simplest way to initialize all elements in a two-dimensional array to 0 is:

Int num [2] [3] = {0 };

When the Declaration statement provides an initial value with all elements, the 1st dimension size can be default, for example:

Int sz [] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12 }};

 

Multi-dimensional array:

If the array is N-dimensional, N subscripts are required to access the elements in the array. Similarly, when declaring a high-dimensional array, in addition to setting Element Types and array names like one-dimensional and two-dimensional array declarations, you must specify the size of each dimension to help the compiler determine the size of memory blocks to be allocated.

For example, to declare an int Type 3-dimensional array sz with a size of 3*4*5, the Code is as follows:

Int sz [3] [4] [5];

Multi-dimensional array initialization:

When declaring a multi-dimensional array, you can also initialize the elements through the initialization expression. Do you still remember the curly braces in the initialization expression in the two-dimensional array? The benefit of using curly brackets is that they are "layered". For example:

Int sz [2] [3] [2] ={{{1, 2}, {3, 4}, {5, 6}},

              {{7, 8}, {9, 10}, {11, 12}}};

 

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.