In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what is the concept of C language array and how to apply it", 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 is the concept of C language array and how to apply it?"
I. the concept of array
An array is an ordered set of variables of the same type
Second, the size of the array
Array stores elements in a continuous memory space
The number of array elements can be displayed or implicitly specified
Let's look at a piece of code for array initialization:
# include int main () {int a [5] = {1,2}; int b [] = {1,2}; printf ("a [2] =% d\ n", a [2]); printf ("a [3] =% d\ n", a [3]); printf ("a [4] =% d\ n", a [4]); printf ("sizeof (a) =% d\ n", sizeof (a)) Printf ("sizeof (b) =% d\ n", sizeof (b)); printf ("count for a:% d\ n", sizeof (a) / sizeof (int)); printf ("count for b:% d\ n", sizeof (b) / sizeof (int)); return 0;}
The following is the output:
Note the calculation of array length, sizeof (array name) / sizeof (array element type)
Array address and array name
The array name represents the address of the first element of the array
The address of the array needs an address character & to get it.
The address value of the first element of the array is the same as that of the array
The address of the first element of an array and the address of the array are two different concepts
Let's take a look at the code for the array name and address:
# include int main () {int a [5] = {0}; printf ("a =% p\ n", a); printf ("& a =% p\ n", & a); printf ("& a [0] =% p\ n", & a [0]); return 0;}
The following is the output:
Note: the address of the first element of the array and the address of the array are two different concepts, although their address values are the same, but the meaning is completely different, the difference is that the length of memory space they occupy is not the same.
IV. Blind spots of array names
The array name can be thought of as a pointer constant
The array name "points" to the starting position of the first element of the array in memory
The array name does not contain the length of the array
The array name can only be used as a right value in an expression
The array name cannot be regarded as a pointer constant only in the following situations
Array name as an argument to the sizeof operator
Array name as an argument to the & operator
The following code shows that arrays and pointers are not the same:
# include int main () {int a [5] = {0}; int b [2]; int* p = NULL; p = a; printf ("a =% p\ n", a); printf ("p =% p\ n", p); printf ("& p =% p\ n", & p); printf ("sizeof (a) =% d\ n", sizeof (a)) Printf ("sizeof (p) =% d\ n", sizeof (p)); printf ("\ n"); p = b; printf ("b =% p\ n", b); printf ("p =% p\ n", p); printf ("& p =% p\ n", & p); printf ("sizeof (b) =% d\ n", sizeof (b)) Printf ("sizeof (p) =% d\ n", sizeof (p)); / / b = a; return 0;}
The output is as follows:
Sizeof (a) = 20 (p) = 4, which shows that pointers and arrays are two different things.
The above is the content of this article on "what is the concept of C language array and how to apply it?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.