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

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. C format controller/placeholder
  6. C language logic operator
  7. C language ternary operator
  8. C language comma expression
  9. C sizeof and strlen are different
  10. C language strcpy and strcpy_s function difference
  11. C memcpy is different from memcpy_S
  12. C language array definition and use
  13. C array traversal
  14. C language array sort – bubble sort
  15. C language array sort – selection sort
  16. C language array sort – insertion sort
  17. C language array sort – fast sort method
  18. C array subscript is out of bounds
  19. C array memory overflow
  20. C array subscript out of bounds and memory overflow difference
  21. C language array length calculation
  22. C language pointer declaration and definition
  23. 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!