In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to create and use arrays in C language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to create and use arrays in C language" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Creation and initialization of one-dimensional array 1. Creation of array
An array is a collection of elements of the same type.
two。 How to create an array
Type_t (array type) arr_name (array name) [const_n] (used to specify the size of the array)
3. Initialization of array
The initialization of an array should be performed when it is defined, such as sorting 5 known shaping data.
/ / correct int Arr [5] = {1,2,3,4,5}; / / not int Arr [5]; Arr [5] = {1,2,3,4,5}
Because for Arr [5] =, this is an assignment operation that assigns the right value to the left value, and all constants, characters, and strings are right values. Here {1, 2, 3, 4, 5}; is not one of the right values, so this is wrong.
Another error is that the element Arr [5] subscript 5 does not actually exist that belongs to the array.
There is initialization of numeric types, and naturally there is initialization of character types.
There are two ways to initialize character types:
Char arr [5] = {'axie'}; char arr [5] = "abcde"
1. The null character is the Terminator of the string (the empty character itself is not part of the string, so the length of the string does not contain null characters)
two。 When manipulating a string, you must ensure that the string ends with an empty character (\ 0) (a sequence of characters that does not end with a null character, not a string).
The use of one-dimensional array
Example
Arrays initialized in single quotation marks represent only this data and do not contain\ 0 at the end
Summary
The array is accessed using the subscript, which starts at 0.
The size of the array can be calculated.
Int sz = sizeof (arr) / sizeof (arr [0])
Storage of one-dimensional array
When we define an integer array of length 5, the operating system assigns it five consecutive memory addresses.
These addresses are used to store data, and the number of bytes occupied by each address is determined by the data type of the array. For example, each address of type int occupies 4 bytes and 8 of type double.
Because each shaping size is four bytes, as shown in the figure, the interval between each address is 4.
After talking about the knowledge of one-dimensional array, let's take a brief look at the two-dimensional array.
Creation and initialization of two-dimensional array 1. Creation of two-dimensional array
The first parenthesis specifies the row and the second parenthesis specifies the column
You can assign values to only some elements, and unassigned elements automatically take a "zero" value. For example:
Int arr [3] [4] = {1pm, 2pm, 3pm, 4pm 5}
two。 Initialization of two-dimensional array
First line: 123 second line: 45
Columns of a two-dimensional array cannot be omitted rows can be omitted when braces are clearly divided
Storage of two-dimensional array
Storage of two-dimensional array in memory
If you define such a two-dimensional array int a [3] [4] = {undefined {1rec 3je 5je 7}, {9pr 11pm 13je 15}, {17pm 19je 21J 23}}, then its representation in memory may be as follows.
As can be seen from the above figure, a two-dimensional array is stored according to the row main order in memory. From a memory point of view, a two-dimensional array is essentially an one-dimensional array. If you treat each row of a two-dimensional array as a whole, that is, as an element in an array, then the whole two-dimensional array is an one-dimensional array. The name of the two-dimensional array represents the first address of row 0 of the two-dimensional array (note that it represents the first address of a row of elements, not the first address of column 0 of row 0, although it is equal, but it cannot be understood this way. so in the case of no cast, two-dimensional data is passed either through a row pointer or through a two-dimensional pointer).
Out of bounds of an array
There is a bound for the length of the array here, and once this size is exceeded, some unknown errors will occur, that is, the so-called out of bounds.
Here is an example to illustrate the values within the array after crossing the bounds:
According to the code, when the array is out of bounds, its value is uncertain.
After reading this, the article "how to create and use arrays in C language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
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.