Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of creating C language array

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the knowledge points of this article "what are the methods of creating a C language array?" most people do not understand, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this article "what are the methods of creating a C language array?"

1. Creation and initialization of one-dimensional array 1.1 creation of array

An array is a collection of elements of the same type.

How the array is created:

Element type array name of the array [constant expression]

Eg. Int arr [5] char ch [100]

Error-prone points in the VS compiler: constant expressions should be inside []

Int n = 5

Int arr [n]; (×)

Int arr [5]; (√)

In fact, the C99 standard does not support the use of variables, only constants! The concept of variable length arrays has been added to C99, which allows the array size to be a variable and requires the compiler to support the C99 standard. VS's support for C99 is not good enough) 1.2 array initialization

Some initial values are called initialization at the same time.

Int arr [5] = {1,2,3,4,5}

Int arr [5] = {1,2,3}; / / incomplete initialization. The remaining elements are initialized to 0 by default.

Int arr [] = {1,2,3}; / / the undetermined size array allocates space according to the initialization content

Char arr1 [] = {'a', 'baked,' c'}; char arr2 [] = "abc"; / / sizeof calculates the array size printf ("% d\ n", sizeof (arr1)); / / arr1 has three elements, and the array size is 3 bytes printf ("% d\ n", sizeof (arr2)) / / arr2 has four elements, the array size is 4 bytes / / strlen to find the string length, stop printf ("% d\ n", strlen (arr1)) when you encounter'\ 0'; / / there is no'\ 0' at the end of the array, we don't know where'\ 0' will appear, so the length of arr1 is a random value printf ("% d\ n", strlen (arr2)). / / at the end of the array, there are three elements before it, and the length of arr2 is 3.

Strlen is a library function. Add # include before using it.

The length of the string is calculated, and only for strings

What we care about is whether there is\ 0 in the string, and the number of characters before\ 0 is calculated.

Sizeof is an operator (operator)

Any type of memory used by sizeof to calculate the memory space occupied by variables can be used.

Only care about the size of the space, do not care whether there is\ 0 in memory.

1.3 use of one-dimensional arrays

The array has a subscript, and the first element has a subscript of 0, which is increased in turn.

Int arr [5] = {1Power2 arr 4, 5}; printf ("% d", arr [2]); / / [] is the following table access operator, where the number with subscript 2 is printed, and all elements of the 3 / / print array are printed, that is, int I = 0; int sz = sizeof (arr) / sizeof (arr [0]) / / 40 take 4 to find out the number of elements, array size for (I = 0; I

< sz; i++) { printf("%d ", arr[i]); }1.4 一维数组在内存中的存储 int arr[5] = { 1, 2, 3, 4, 5 }; //打印数组每个元素的地址 int i = 0; for (i = 0; i < 5; i++) { printf("&arr[%d] = %p \n",i, &arr[i]); }

The difference between every two addresses is 4.

An integer is four bytes

Give an address a byte in memory

Conclusion

1. One-dimensional arrays are stored continuously in memory.

two。 The address of the array changes from low to high as the subscript grows.

Int arr [5] = {1,2,3,4,5}; int I = 0; int * p = & arr [0]; for (I = 0; I < 5; iTunes +) {printf ("% p -% p\ n", & arr [I], p + I);}

You can use the first address + I to jump to the I element address.

So you can get the first element with * (pallei) (this has something to do with the pointer to be talked about later, so let's take a look at it now)

two。 Creation and initialization of 2D arrays 2.1 creation of 2D arrays int arr [3] [4]; char arr [3] [5]; double arr [2] [4]

Int arr [3] [4]

2.2 initialization of two-dimensional array int arr [3] [4] = {1pr 2je 3je 4}; / / if it is not fully initialized, add 0int arr [3] [4] = {{1pr 2}, {4pc5}} / / 1 200 / / 4 500 / / 000 0int arr [] [4] = {{2jue 3}, {4pm 5}} / / if the two-dimensional array is initialized, the row can be omitted, and the column cannot be omitted. 2.3.The use of the two-dimensional array / / print the two-dimensional array int arr [3] [4] = {1,2,3,4,5,6,7,8,9,10,11,12}; int I = 0; for (I = 0; I < 3; iTunes +) {int j = 0 For (j = 0; j < 4; jacks +) {printf ("% d", arr [I] [j]) The address of each element int arr [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int I = 0; for (I = 0; I < 3; iTunes +) {int j = 0 For (j = 0; j < 4; jacks +) {printf ("& arr [% d] [% d] =% p\ n", iMagnej, & ARR [I] [j]);}}

Two-dimensional array storage seems to be discontinuous, but in fact it is continuous.

3. Array out of bounds

The subscript of an array is limited in scope.

The lower stipulation of the array starts at 0, and if the array has n elements, the subscript of the last element is nMel 1.

So if the subscript of the array is less than 0 or greater than nMel 1, the array is out of bounds and beyond the legal space of the array.

The C language itself does not cross the bounds of the array subscript, and the compiler does not necessarily report an error, but the fact that the compiler does not report an error does not mean that the program is correct.

Rows and columns of a two-dimensional array may also be out of bounds.

So when programmers write code, it's best to check out of bounds themselves.

# include int main () {int item0; int arr [] = {1, 2, 4, 5, 6, 8, 9, 10}; for (iMag0; I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report