In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will show you how to understand the two-dimensional array in C language. the knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to understand two-dimensional arrays in C language".
Two-dimensional array: introduce an idea: a variable: equivalent to a point. One-dimensional array (that is, array): points converge into a straight line. Two-dimensional array: straight lines converge into a plane. Three-dimensional array: planes converge into three-dimensional graphics. Syntax:
Type array name [constant expression]
Int a [6] [6]; / / 6 rows and 6 columns
Char b [4] [5]; / / 4 rows and 5 columns
Double c [6] [3]; / / 6 rows and 3 columns
Storage method of two-dimensional array:
Access to two-dimensional arrays:
Array name [subscript] [subscript]
A [0] [0]; / / access the elements of the first row and first column of the an array
B [1] [3]; / / access the elements of the second row and the fourth column in the b array
C [3] [3]; access the elements of the fourth row and fourth column of the c array
Also pay attention to the range of subscript values to prevent out-of-bounds access to the array.
-for example, int a [3] [4], the value range of "row subscript" is 0 ~ 2, and the value range of "column subscript" is 0 ~ 3. Access beyond any subscript is out of bounds. (it's very easy to win.)
Initialization of two-dimensional array:
Two-dimensional arrays are stored linearly in memory, so you can write all the data in a curly bracket:
Int a [3] [4] = {1 recorder 2 3 3 4 5 6 7 8 9 10 11 12]
For a more intuitive representation of the distribution of elements, you can enclose the elements of each line in curly braces:
Int a [3] [4] = {{1Perry 2jorn 3je 4}, {5pje 6pr 7pr 8}, {9rect 10pm 11jue 12}}
Or
Int a [3] [4] = {{1Perry 2jorn 3je 4}, {5pje 6pr 7pr 8}, {9rect 10pm 11jue 12}}
A two-dimensional array can also assign initial values to only some elements:
Int a [3] [4] = {{1}, {5}, {9}}
If you want the entire two-dimensional array to be initialized to 0, just write a 0 in curly braces:
Int a [3] [4] = {0}
C99 also adds a new feature: specify initialized elements. In this way, only some specified elements in the array can be initially hashed, and the elements that are not assigned can be automatically initialized to 0:
Int a [3] [4] = {[0] [0] = 1, [1] [1] = 2, [2] [2] = 3}
Initialization of a two-dimensional array can also be lazy, allowing the compiler to calculate the length of the array based on the number of elements. However, only the number of elements in the first dimension can not be written, and other dimensions must be added:
Int a [] [4] = {{1pm 2je 3je 4}, {5pm 6pr 7pm 8}, {9pm 10pm 11je 12}}
Example:
Example 1: comfortable writing of two-dimensional array:
1 # include 2 int main () 3 {4 int a [3] [4] = {5 {1 for 2 for 3 for 4}, 6 {5 int 7 minus 7 mins 8}, 7 {9 int 10 pines 11 beats 12} 8}; 9 int iParry j; 10 beacons (iMagi)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.