Getting Started: Decrypting array name essence

Source: Internet
Author: User

Now it is time to uncover the nature of the array name, giving three conclusions first:

(1) The meaning of the array name is that its reference entity is a data structure, which is an array;

(2) The extension of the array name is that it can be converted to a pointer to its reference entity, and is a pointer constant;

(3) The pointer to the array is another type of variable (under the WIN32 platform, the length of 4), just means the storage address of the group!

1, the array name refers to a data structure: array

Now you can explain why the 1th program has 10 output in line 6th, according to conclusion 1, the meaning of array name STR is a data structure, a char array of length 10, so the result of sizeof (STR) is the amount of memory occupied by this data structure: 10 bytes.

Look again:

1. int intarray[10];

2. cout < < sizeof (Intarray);

The output of line 2nd is 40 (the amount of memory space occupied by an integer array).

If you can write this in C + + programs:

1. int[10] Intarray;

2. cout < < sizeof (Intarray)

, we all understand that intarray is defined as an example of this kind of data structure, but unfortunately, C + + does not currently support this type of definition int[10.

2, the array name can be used as a pointer constant

According to conclusion 2, the array name can be converted to a pointer to its reference entity, so the 5th row of the array name in Program 1 is directly assigned to the pointer, and program 2, line 7th, directly assigns the array masterpieces as pointer parameters.

Is the following procedure set up?

1. int intarray[10];

2. intarray++;

The reader can compile it and discover a compilation error. The reason is that although an array name can be converted to a pointer to its reference entity, it can only be treated as a pointer constant and cannot be modified.

and pointers, whether pointing to the structure, array or basic data types of pointers, do not contain the original data structure of the content, in the WIN32 platform, sizeof operation results are 4.

By the way, correct another misunderstanding by many programmers. Many programmers think that sizeof is a function, but in fact, it is an operator, but its use does look like a function. The statement sizeof (int) explains that sizeof is indeed not a function, because the function accepts the formal parameter (a variable), and there is no C + + function in the world that accepts a data type (such as int) as a "formal parameter".

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.