Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the concept of C language array and how to apply it

Shulou Source: shulou.com Published: 2022-05-31 21:02:23 09月14日 Update

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.

Tags: Array address element concept content different same pointer language application two code memory result length output parameter masterpiece constant article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology NVidia Shulou Information Shulou Tech Info