Home > Common Problem > How to define array in c language

How to define array in c language

清浅
Release: 2020-09-08 17:40:33
Original
125408 people have browsed it

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.

How to define array in c language

[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};
Copy after login

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 }  };
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template