Discussion on the interoperability between soul arrays and pointers in C Language

Source: Internet
Author: User
Tags array definition
Discussion on the interoperability of soul arrays and pointers in C language-general Linux technology-Linux programming and kernel information. The following is a detailed description. I have heard from many friends that C is a nostalgic language because it has a long history. However, since the emergence of various object-oriented programming languages, its influence has been diminished.

Of course, this is beyond review, but the efficiency of C is comparable to that of other languages. Therefore, it is necessary for us to grasp the essence and mysteries of C, and to know the basic data structures such as arrays, stack, list, struct, and other operations and implementations. Pointer is also an excellent and flexible structure in C language, and its understanding is also essential.

We generally think that arrays are a one-dimensional data storage structure, because two arrays or matrices can be seen as a combination structure of multiple one-dimensional arrays, the data storage access method defined on it is the same. Therefore, a one-dimensional array is the most basic and important part. Only by understanding the nature of such a data structure can we bypass the category.

An array is an aggregation of several similar variables. You can use a uniform name to consume these variables. Therefore, an array is a finite set of data of the same type. You can use the following table to access an array element.

In C language, arrays are composed of consecutive memory areas (sometimes, not necessarily). The lowest address corresponds to the first element, and the subscript of the array starts from 0, so the first element is the element whose subscript is 0, the highest address corresponds to the last element, that is, the N-1 element (if the array we define is N yuan ).

Array definition method:

In C language, you can initialize an array while declaring it. You can also place the Declaration and definition in different positions. The Initialization is generally similar to the following expression:

Type_specifier array_name [size1]... [sizeN] = {value_list };

Vlaue_list is a constant table separated by commas (,). The constant table must be compatible with type_specifier. Separated by semicolons (;) and the next statement. The definition of a one-dimensional array is as follows:

Type_specifier

Array_name [size] = {value_list };

As follows:

Char hello [12] = {'h', 'E', 'l', 'l', 'O ',','

, '', 'W', 'O', 'R', 'l', 'D', '\ 0 '};

Note: The character array ends with a "'\ 0'", which is part of the C standard. When operating the character array, '\ 0' is used as the marker to end the judgment. Of course, if you define a string, you don't need to add this '\ 0. Because there is a mechanism to help you automatically add.

In the above example, the string's lifecycle is: string hello = "Hello, world"; (of course, you must set "string. h "add the header file to your file), or you can declare it as follows:

Char * hello = "Hello, world"; or char hello [] = "Hello, world ";

Switch to a character pointer array, which has the same effect). During array initialization, the maximum size is not indicated, that is

Char hello [] = {'h', 'E', 'l', 'l', 'O'

, ',', '', 'W', 'O', 'R', 'l', 'D ','

\ 0 '};,

At this time, the compiler will allocate suitable memory space for the Array Based on the Value assignment. You don't have to worry about this unless the machine is in a memory shortage status.

Access to array elements:

You can use the loop structure to access array elements one by one, for example:

[...]

Int I;

Char hello [12] = {'h', 'E', 'l', 'l', 'O ','

, ', '', 'W', 'O', 'R', 'l', 'D',' \ 0 '};

[...]

For (I = 0; I <12; I ++ ){

Printf ("% c", hello );

}

Printf ("\ n ");

[...]

Note that the value of I cannot be 12, because the lower part of our logo starts from 0, that is, hello [0] is the first element and the lower bound of the array, hello [12] is the first empty element, the upper bound of the array.

In fact, the number of array elements is equal to the subscript of the definition, it is also equal to the value of the upper bound (12) of the array minus the lower bound (0, it is also equal to the value of the upper-bound address minus the lower-bound address modulo sizeof (tyep_specifier) (assuming that the array space is continuously distributed, otherwise this method will not work ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.