In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The purpose of this article is to share with you what C language array means. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is an array?
Array is an ordered linear structure table, which is used to store the same type of collection. And the addresses of array elements are contiguous.
The greatest advantage of an array is that it supports random access. When you want to access an array, you only need to find the corresponding subscript of the array to find the corresponding elements of the array. But the array also has the corresponding disadvantage, that is, the number of elements and the size of the array space are fixed at the time of creation, if the array space is not used up, it will cause space waste, and because the address of the array is contiguous, this should be an advantage, but this causes the array to need O (n) to delete or add elements.
The subscript of the array
The array subscript starts at 0, assuming that when you access the arr [5] element, you access the sixth element of the array, and when you access arr [0], you access the first element of the array.
One-dimensional array
One-dimensional array creation
One-dimensional array is a common array, which is created by: data type + array name [number of elements]
Int arr [10]
Before the C99 standard, when an array was created, the number of array elements in square brackets could only be constant, and the constant used must be a real constant. For example, a variable modified with const has a constant attribute, but it is not a real constant, while the constant defined by define can be used as the number of array elements. The elements in square brackets after C99 can use variables, and the array that uses variables as the number of array elements is called a variable-length array.
One-dimensional array initialization
Int arr [10] = {1, char str, 2, 4, 5, 7, 7, 8, 9, 10}; / / the shaping array int arr [] = {0} / / does not specify a size but must initialize char ch [10] = {'1]; / / character array char str [10] = "abcde"; / / string array
Assignment of one-dimensional array
Use circular variables to access the array subscript and assign values to the array. % s corresponds to entering a string and needs to provide an array of characters to store. The array name is an address, so there is no need to add & take the address.
Int arr [10] = {0}; for (int I = 0 Ted I
< 10;i++){ scanf("%d",&arr[i];} char str[10] = {0};scanf("%s",str);//字符串赋值可以不取地址和循环 一维数组在内存的存储方式 一维数组的元素地址是连续的,也就是元素的地址一个紧挨着一个。地址在内存是以二进制进行存储的,但是如果以二进制来展示的话就会非常的长而且也未必好理解,所以就由十六进制来进行展示。(下图)观察下图可发现,地址的是由低到高随着下标增长而增长的,有规律的递增,且每个数组元素地址都相差了四个字节,相差的四个字节是一个int整形的空间大小。Two-dimensional array
A two-dimensional array can be a matrix in our logical concept, but it is a continuous address space in memory as well as an one-dimensional array. Usually we see the first square bracket of a two-dimensional array as a row and the second square bracket as a column.
Creation and initialization of two-dimensional array
The two-dimensional array and the one-dimensional array just add an extra square bracket. Data type + array name [number of elements] [number of elements]
Int arr [3] [3] = {1 int arr [3] = {1] [0] automatically saved to the position of int arr [1] [0], int arr [3] [3] = {{1, 5, 5, 6}, {7], 8, 9}}; / / A curly braces represent a line, each braces are separated by commas int arr [] [3] = {0} / / A two-dimensional array may not initialize rows, but columns must be initialized
Assignment of two-dimensional array
Assigning values to a two-dimensional array requires the same loop as an one-dimensional array, but a two-dimensional array needs to nest a layer of loops on top of the loops of the one-dimensional array.
Int arr [3] [3] = {0}; for (int I = 0 X I < 3 x I +) {for (int j = 0 x j < 3 x j +) {scanf ("% d", & ARR [I] [j]); / / I access row, j access column}} array out of bounds
The subscript range of the array is limited, because the subscript of the array starts with 0, so the subscript that the array can access is the number of array elements minus one (NMel 1). When you visit a space that does not belong to the address range of the array elements, it is called array out of bounds. Suppose an array of arr [10], when the access subscript is greater than or equal to 10, will cause the array to overflow backward, also known as downward overflow. On the other hand, when the array crosses the boundary to the front of the first element of the array, that is, the subscript is less than 0, it is called upper overflow. Cross-boundary access is a very dangerous operation, because some compilers do not check whether the array is out of bounds, so When programmers are writing code, they should pay close attention to whether the array is out of bounds. The same is true for rows and columns of a two-dimensional array
Array name
The array name is a pointer to the address of the first element of the array, that is, the address of the element whose subscript is 0. As mentioned above, because the address of the array is contiguous, when you find the first element of the array, you can find the other members of the array. If you use sizeof (array name), the array name here represents the entire array and calculates the size of the entire array. Divide by sizeof (the address with subscript 0) to get the number of elements in the array.
The mode of passing parameters to the array
When you want to pass an array as a function parameter, you need to put an array name in the position where the function is passed, and the parameter part will receive a pointer to the address of the first element of the array, and a pointer of the same type is needed to receive this pointer. In the formal parameter part, the array can be expressed in two forms, one is in the form of an array, the other is in the form of a pointer, both of which can pass parameters to the array.
Void bubble_sort (int arr []) void bubble_sort (int* arr)
When the array is passed parameters, the function part cannot calculate the number of elements of the array. Because the formal parameter only receives a pointer to the address of the first element of an array, not the whole array, when calculating the array, it only calculates the size of the address of the first element of the array, and then divides it by the address of the first element. so of course you get a 1. Therefore, when the array needs to pass parameters, and also needs to use the number of elements of the array, we should first calculate the number of elements of the array, and pass the number of elements together with the array.
Thank you for reading! This is the end of the article on "what is the meaning of C language array". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.