This is the 23rd day of my participation in the August More Text Challenge.More challenges in August
directory
- Calculate the length of a one-dimensional array
- Calculate the length of a two-dimensional array
- 1. Number of rows in a two-dimensional array
- 2. Number of columns in a two-dimensional array
- 3. Number of elements in a two-dimensional array = number of rows in a two-dimensional array x number of columns in a two-dimensional array
- Guess you like it
C/C++ learning Directory >> C language basics
Calculate the length of a one-dimensional array
For an array of the form type array[A], we can calculate the sizeof the array by evaluating sizeof, as an example:
int len = sizeof(array)/sizeof(array[0]);
Copy the code
Calculate the length of a two-dimensional array
For A two-dimensional array of type array[A][B], we can calculate the number of rows/columns of the array by calculating sizeof.
1. Number of rows in a two-dimensional array
Sizeof (array)/sizeof(array[0]);Copy the code
2. Number of columns in a two-dimensional array
Sizeof (array[0])/sizeof(array[0][0]);Copy the code
3. Number of elements in a two-dimensional array = number of rows in a two-dimensional array x number of columns in a two-dimensional array
As shown in the figure above, we can calculate the number of rows and columns of a two-dimensional array using the principle introduced above, and iterate over the two-dimensional array with the following example code:
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - two-dimensional array traversal // @time :2021/06/12 08:00 // @motto: No accumulation of small steps no thousands of miles, no accumulation of small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include<stdlib.h> #include<stdio.h> void main() { int rows = 0; Int columns = 0; / / the number of columns int arr [3] [4] = {{1 hc-positie}, {9,11,13,15}, {17,19,21,23}}; rows = sizeof(arr) / sizeof(arr[0]); columns = sizeof(arr[0]) / sizeof(arr[0][0]); Printf (" an array of rows: number of columns: % d % d total number of elements: % d * % d = % d \ n ", rows, columns, rows, columns, rows * columns). for (int i = 0; i<rows; i++) { for (int j = 0; j<columns; j++) { printf("arr[%d][%d] = %d \n",i,j,arr[i][j] ); } printf("\n"); } system("pause"); } /* Output: array rows: 3 columns: 4 total elements: 3 * 4 = 12 arr[0][0] = 1 arr[0][1] = 3 arr[0][2] = 5 arr[0][3] = 7 arr[1][0] = 9 arr[1][1] = 11 arr[1][2] = 13 arr[1][3] Arr = 15 [2] [0] = 17 arr [2] [1] = 19 arr [2], [2] = 21 arr [2] [3] = 23 please press any key to continue... * /Copy the code
Guess you like it
- Install Visual Studio
- Install the Visual Studio plug-in, Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 Uninstall
- C format controller/placeholder
- C language logic operator
- C language ternary operator
- C language comma expression
- C sizeof and strlen are different
- C language strcpy and strcpy_s function difference
- C memcpy is different from memcpy_S
- C language array definition and use
- C array traversal
- C language array sort – bubble sort
- C language array sort – selection sort
- C language array sort – insertion sort
- C language array sort – fast sort method
- C array subscript is out of bounds
- C array memory overflow
- C array subscript out of bounds and memory overflow difference
- C language array length calculation
- C language pointer declaration and definition
- C language two dimensional array traversal
C language two-dimensional array traversal
This article is published by the blog – Ape Say Programming Ape Say programming!