Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Array in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about the sample analysis of arrays in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Array

An array is an ordered collection of variables of the same type, used to hold a set of data of the same type. This set of variables is identified by an array name and a subscript starting at 0, using a contiguous block of storage space in memory. According to the number of subscripts of elements in the array, it is divided into one-dimensional array, two-dimensional array and multi-dimensional array.

1.1 one-dimensional array 1.1.1 definition of one-dimensional array

The general form of an one-dimensional array definition is:

Type specifier array name [constant or constant expression]

Once an array is defined, its length cannot be changed, or the number of elements in the array is fixed. When the program is compiled or run, the array is allocated a contiguous piece of storage of a fixed size. For example:

Int array [10]

Define an one-dimensional array array, with the data element of type int, and there are 10 elements in the array. The subscript of these ten elements ranges from 0 to 9.

1.1.2 initialization of one-dimensional array

(1) assign initial values to all elements of the array. Such as:

Int a [10] = {1,2,3,4,5,6,7,8,9,10}

(2) initialize all elements of the array. The array length defaults, and the number of initial values is the array length. Such as:

Int a [] = {1,2,3,4,5,6,7,8,9,10}

(3) assign initial values to some elements of the array. Such as:

Int a [10] = {1,2,3,4,5}

Only the first five elements of the array are assigned initial values, and the initial value of a [0] is 1,. , the initial value of a [4] is 5, and the initial value of other elements is 0.

If the array is not initialized, the initial value of the array element is an uncertain value.

1.1.3 references to one-dimensional arrays

[example] the elements in the array are stored in reverse order and output (no additional array storage space is allowed).

# include#define N 10void main () {int I, t, a [N]; for (I = 0; I

< N; i++) { scanf("%d", &a[i]); //为数组元素赋值 } printf("交换前的数组元素\n"); for(i = 0; i < N; i++) //输出交换前数组元素 { printf("%6d", a[i]); } for(i = 0; i < N/2; i++) //交换 { t = a[i]; a[i] = a[N - i - 1]; a[N - i - 1] = t; } printf("\n交换后代的数组元素\n"); for(i = 0; i < N; i++) //输出交换后数组元素 { printf("%6d", a[i]); } printf("\n");} 运行及结果如下图所示: 1.2 二维数组及多维数组1.2.1 二维数组的定义 二维数组定义的一般形式为: 类型说明符 数组名 [常量表达式1] [常量表达式2]; 两个方括号中的常量表达式1和2分别规定了数组第一维的长度和第二维的长度,也称为行数和列数。两个常量表达式的乘积是数组元素的个数。 事实上,二维数组是由一维数组扩展而来的。若一维数组的元素本身还是一个一维数组,则构成二维数组。以此类推,可构成三维数组,乃至多维数组。 1.2.2 二维数组的初始化 (1)按行为二维数组元素赋初值。如: int a [3] [5] = {{0, 1, 2, 3, 4}, {1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}}; (2)使用一维数据对二维数组元素赋初值。如: int a [4] [5] = {0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6}; (3)按行为二维数组部分元素赋初值。如: int a [4] [5] = {{1, 3}, {2}, {0}, {0, 2, 4}}; (4)当对全部元素初始化或按行初始化时,第一维的长度可以缺省,但第二维的长度必须注明。如: int a [] [4] = {1, 2, 3, 4, 5, 6, 7, 8} int b [] [4] = {{1, 2, 3}, {1, 2, 3}} 1.2.3 二维数组的引用 【例】对5*5的矩阵编程。 (1)求出主对角线元素的和; (2)求出矩阵下三角元素的和; (3)找出主对角线上最大值元素和它的位置。

Thank you for reading! This is the end of this article on "sample analysis of arrays in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report