The definition of arrays in C language has four forms: 1. The form with only array type and array name; 2. The form with determined array length; 3. Use the new keyword to allocate memory to the array method; 4. Use the new keyword and assign a value to the array.
[Recommended course: C Language Tutorial]
Definition of array
Array refers to a collection used to store the same type of data. An array is actually a series of variables. Arrays can be divided according to their use. For one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays
Array definition methods
There are four forms of array definition methods, namely:
(1) Type name [] Array name;
(2) Type name [] Array name = { Initial value list };
(3) Type name [] Array name = new Type name [array size];
(4) Type name [] array name = new type name [array size] { initial value list };
Data type: refers to the array element Data types, common ones include integer, floating point, character, etc.
Array name: It is used to unify the name of this group of elements of the same data type, and its naming rules are the same as those of variables
Connection Next, I will introduce how to define an array through specific examples
Definition of one-dimensional array
int[] nullArray; int[] intArray = new int[ 10 ]; double[] mathConsts = { 3.14,2.71828,1.414 }; Days[] holiday = new Days[ 2 ]{ Days.Sat, Days.Sun};
Definition of two-dimensional array
In some cases, one-dimensional arrays can no longer meet the needs of the application. When we need to record data in a table, it will be very inconvenient to use one-dimensional arrays because we need to use two-dimensional arrays
Type name[,] Array name;
Type name[,] Array name = { Initial value list};
Type name[,] Array name = new Type name[line , column];
type name[,] array name = new type name[row, column] {initial value list};
int[,] intArray = new int[ 10, 20 ]; double[ 2, 3 ] mathConsts= { { 3.14, 2.71828, 1.414 }, { 0.5, 0.25, 0.125 } }; Days[,] workDays = new Days[ 2, 2 ] { { Days.Mon, Days.Wed }, { Days.Tue, Days.Thu } };
Summary: The above is the entire content of this article ,I hope to be helpful.
The above is the detailed content of How to define array in c language. For more information, please follow other related articles on the PHP Chinese website!