An elementary introduction to the interaction between arrays and pointers in C language

Source: Internet
Author: User

I've heard a lot of friends say that C is a nostalgic language because it has a long history, but its influence has been reduced since the advent of various object-oriented programming languages.

Of course, this is justifiable, but the efficiency of C is comparable to other languages, so we have to grasp the essence and mystery, it is necessary to know the basic data structure such as arrays, a little bit depth of the stack, list, structure and other operations and implementation. The pointer is also a very good flexible structure in C language, and it is essential to understand it.

We generally think of arrays as one-dimensional data storage structures, because two-bit arrays or matrices can be considered as a composite structure of multiple one-dimensional arrays, and the data stored on them are accessed in the same way. So the one-dimensional array is the most important part of it, only the understanding of the nature of such data structure can be analogy.

An array is an aggregation of several homogeneous variables that allow you to drink the variables in a uniform name. So an array is a finite set of data of the same type. You can access one or more of the array elements in the array by using the following table.

In C, arrays are composed of contiguous memory regions (sometimes, this is not necessarily the case, the lowest address corresponds to the first element, and the subscript of the array begins at 0, so the first element is the element with the array subscript 0, and the highest address corresponds to the last element, the N-1 element (if we define an array of n-ary).

How arrays are defined:

The C language allows you to initialize the array at the same time as you declare it, or you can place the declaration and definition in a different place, initializing a generic expression similar to the following:

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

Where vlaue_list is a constant table separated by commas (,), the constant table must be compatible with Type_specifier. The last semicolon is separated from the next statement. This shows that one-dimensional arrays are defined in the following ways: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 is a "' Yes '" ending, which is part of the C standard. Because when manipulating a character array, it is marked as the end of the judgment. Of course, if you're defining a string, you don't have to add this ' yes '. Because there are mechanisms to help you automatically add.

The string of life in the example above is: string hello = "Hello, world";(of course, you must add the "string.h" header file to your file in the implementation, or you can declare:

char *hello = "Hello, world";
或者
char hello[] = "Hello, world";

Switch to an array of character pointers, the effect is the same), and the initialization of the array can also not indicate the maximum small, that ischar hello[] = {’H’,’e’,’l’,’l’,’o’
,’,’,’ ’,’w’,’o’,’r’,’l’,’d’,’\0’};,

The compiler then assigns the appropriate memory space to the array based on the following assignment, which you don't have to worry about unless the machine is in a memory shortage state.

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.