In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how the C language to achieve the creation and initialization of one-dimensional array, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
Creation of one-dimensional array and initialization of 1 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) 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.
3 use of one-dimensional array
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]); }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; i++) { 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).
These are all the contents of the article "how to create and initialize one-dimensional arrays in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.