Home  >  Article  >  Backend Development  >  Definition and assignment of arrays in C language

Definition and assignment of arrays in C language

王林
王林Original
2020-05-09 10:57:5717520browse

Definition and assignment of arrays in C language

One-dimensional array is defined as follows:

类型说明符 数组名[常量表达式];

For example:

int a[5];

It means that an integer array is defined , the array is called a, and the defined array is called array a.

At this time, there are 5 elements in array a, each element is an int type variable, and their addresses in the memory are allocated continuously. In other words, if an int type variable occupies 4 bytes of memory space, then 5 int type variables occupy 20 bytes of memory space, and their addresses are allocated continuously.

One-dimensional array initialization

The initialization of one-dimensional array can be achieved using the following methods:

1. Assign initial values ​​to all elements when defining the array , this is called "full initialization".

For example:

int a[5] = {1, 2, 3, 4, 5};

By placing the initial values ​​of the array elements in a pair of curly braces, after initialization, a[0]=1; a[1]=2; a [2]=3; a[3]=4; a[4]=5, that is, assigned to each element from left to right. It should be noted that during initialization, each element is separated by commas, not semicolons.

2. You can assign values ​​to only part of the elements, which is called "incomplete initialization".

For example:

int a[5] = {1, 2};

The array a defined has 5 elements, but only two initial values ​​are provided within the curly braces, which means that only the first two elements a[0] are given , a[1] is initialized, and the following three elements are not initialized. When incompletely initialized, elements that have not been initialized are automatically set to 0.

Recommended tutorial: c language tutorial

The above is the detailed content of Definition and assignment of arrays in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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