What are the operation methods of C language array
This article mainly explains "what are the operation methods of C language array". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the operation methods of C language array"?
One-dimensional array 1. Create / / how to create an array int num [10]; char arr [10]; double sum [10]; float fix [10]; / / variable length array / / array size is variable / / C99 syntax supports int a = 10 int arr [a]; 2. Initialize int arr [3] = {1rec 2jol 3}; / / int Array Type / / arr Array name / / [3] Array has 3 elements / / {1Power2Pol 3} initialization element is 1JI 2p3.
3. Use # includeint main () {int arr [10] = {0}; / initialize each element to 0 / / the array is accessed by subscript / / the subscript is int arr [4] = 5 printf / assign the element with subscript 4 to a value of 5 printf ("% d\ n", arr [4]); / / output the value return 0 of the fifth element in the array } / / the result is 54. Storage of arrays in memory
5. Calculate the size of the array int arr [10]; int sz = sizeof (arr) / sizeof (arr [0]); / / sizeof (arr) calculates the total size of the array / / sizeof (arr [0]) calculates the size of the first element / / total size / size of the first element = the number of elements 2, two-dimensional array 1. Create / / create an array of 3 rows and 3 columns int arr [3] [3]; char sum [3] [3]; / / [] the part of the row can be omitted, the column cannot be int arr [] [3]; char sum [] [3]
two。 Initialize / / create a two-dimensional array with / / 3 rows and three columns with 9 elements int arr [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int sum [] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
3. Use
The subscript of the row and column of a two-dimensional group also starts at 0.
# includeint main () {int arr [3] [3] = {1 int arr [3] = {1 for (I = 0; I) traversing two-dimensional array for (I = 0; I)
< 3; i++) { for (j = 0; j < 3; j++) { //输出行和列元素组成的坐标 printf("%d ", arr[i][j]); } printf("\n"); } return 0;} 输出结果:
4. Storage of two-dimensional arrays in memory # includeint main () {int arr [3] [3] = {1meme 2maijen 3maijen 5Ling 6Ling 7pi 8je 9}; int I = 0; int j = 0; for (I = 0; I)
< 3; i++) { for (j = 0; j < 3; j++) { printf("&arr[%d] [%d] = %p\n", i, j, &arr[i][j]); } printf("\n"); } return 0;} 输出结果: 二维数组在内存中也是连续存放的 一行是连续,跨行也是连续 三、数组作为函数参数1.关于数组名是数组首元素的地址的两个例外 (1)sizeof[数组名] - 数组名表示的是整个数组 - 计算的是整个数组的大小 - 单位是字节。 (2)&数组名 - 数组名表示整个数组 - 取出的是整个数组的地址。 2.冒泡排序 (1) 冒泡排序的思想:两两相邻的元素进行比较,并且可能会进行交换
(2) Code example
# includevoid bubble_sort (int arr [], int sz) / / Parametric arr is essentially a pointer {/ / determine the number of trips int I = 0; / / number of trips for (I = 0; I
< sz - 1; i++) { //一趟冒泡排序的过程 int j = 0; //判断相邻的两个数的大小,并交换 //每一趟结束后要排的数字都会减少一个 for (j = 0; j < sz - 1 - i; j++) { if (arr[j] >Arr [j + 1]) {/ / Exchange int tmp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = tmp;} int main () {int I = 0; int arr [] = {5pm 4pm 3je 1J 0} / / sort in ascending order-Bubble sort / / calculate the number of elements in the array int sz = sizeof (arr) / sizeof (arr [0]); bubble_sort (arr, sz); / / when the function of the bubble sort is called / / the array passes parameters, / / the address of the first element of the array is actually passed for (I = 0; I < sz) ITunes +) {printf ("% d", arr [I]);} return 0;}
Output result:
0 1 2 3 4 5
At this point, I believe you have a deeper understanding of "what are the operation methods of C language array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!