In-depth exploration of differences between C/C ++ array names and pointers

Source: Internet
Author: User

Introduction

Pointers are characteristic of the C/C ++ language, while array names are too similar to pointers. In many cases, array names can be used as pointers. As a result, many program designers are confused. Many university teachers, in their C language teaching process, also had to explain to students: "array name is Pointer ". Fortunately, my college teacher is one of them. Today, I am developing C/C ++ projects day after day, and I am still surrounded by programmers who keep the misunderstanding that "array name is Pointer.

Presumably, this misunderstanding is rooted in a famous Chinese C Programming Tutorial. If this article can correct the misunderstanding of array names and pointers by many Chinese programmers, I am not very pleased. In this article, I stand among countless Chinese programmers who are hungry for knowledge and deeply hope to write computer books in China, I hope you can take the book writing work with a "in-depth exploration" approach and a serious attitude towards perfection. I hope there will be more painstaking efforts integrated into the author's thinking!

  Magic array name

Please refer to the Program (this program is compiled on the Win32 platform ):

1. # include <iostream. h>
2. Int main (INT argc, char * argv [])
3 .{
4. Char STR [10];
5. char * pstr = STR;
6. cout <sizeof (STR) <Endl;
7. cout <sizeof (pstr) <Endl;
8. Return 0;
9 .}

1. array name is not a pointer

Let's first overturn the argument that "array name is Pointer" and use reverse identification.

Verify that the array name is not a pointer

Assume that the array name is a pointer;

Then: Both pstr and STR are pointers;

This is because the pointer length is 4 on the Win32 platform;

Therefore, the output of rows 6th and 7th should be 4;

The actual situation is: 6th rows output 10, 7th rows output 4;

So: if not, the array name is not a pointer.

2. the array name is like a pointer.

We have proved that the array name is indeed not a pointer, but let's look at the 5th rows of the program. This row of programs directly assign the array name to the pointer, which seems that the array name is indeed a pointer!

We can also find examples of array names that look like pointers:

1. # include <string. h>
2. # include <iostream. h>
3. Int main (INT argc, char * argv [])
4 .{
5. Char str1 [10] = "I love u ";
6. Char str2 [10];
7. strcpy (str2, str1 );
8. cout <"String Array 1:" <str1 <Endl;
9. cout <"String Array 2:" <str2 <Endl;
10. Return 0;
11 .}

The two parameters that can be accepted in the original form of the strcpy function in the Standard C library are char pointers, but what we pass to it in the call is two array names! Function output:

String Array 1: I love u
String Array 2: I love u

The array name looks like a pointer again!

Since the array name is not a pointer, why use the array name as a pointer everywhere? As a result, many programmers come to the conclusion that the array name (main) is (that) Not a pointer (object ).

The entire Devil.

  Secret array name

Now it is time to reveal the essence of the array name. Let's draw three conclusions:

(1) The meaning of array name is that it refers to an object as a data structure, which is an array;

(2) the extension of an array name is that it can be converted into a pointer pointing to its object, and it is a pointer constant;

(3) the pointer to the array is another variable type (in Win32 platform, the length is 4), which only means the address of storing the array!

1. array name represents a Data Structure: Array

Now we can explain why the output of 1st rows of 6th programs is 10. According to conclusion 1, the meaning of the array name STR is a data structure, that is, a char array with a length of 10, therefore, the result of sizeof (STR) is the memory size occupied by the data structure: 10 bytes.

Let's look at it again:

1. Int intarray [10];
2. cout <sizeof (intarray );

The output result of rows 2nd is 40 (The memory size occupied by integer arrays ).

If the C/C ++ program can be written as follows:

1. Int [10] intarray;
2. cout <sizeof (intarray );

We all understand that intarray is defined as an instance of the int [10] data structure. Unfortunately, C/C ++ currently does not support this definition method.

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

According to conclusion 2, the array name can be converted to a pointer pointing to the object. Therefore, the array name of row 5th in program 1 is directly assigned to the pointer, the array name can be directly used as a pointer parameter in line 2 and 7th.

Is the following program established?

1. Int intarray [10];
2. intarray ++;

You can compile it to discover compilation errors. The reason is that although the array name can be converted to a pointer pointing to the object, it can only be regarded as a pointer constant and cannot be modified.

Pointers, whether directed to struct, array, or basic data types, do not contain the meaning of the original data structure. On the Win32 platform, the sizeof operation results are 4.
By the way, we can correct another misunderstanding of many programmers. Many Programmers think that sizeof is a function. In fact, it is an operator, but its usage does look like a function. The statement sizeof (INT) indicates that sizeof is indeed not a function, because the function accepts the form parameter (a variable ), no C/C ++ function in the world accepts a data type (such as INT) as a "shape parameter ".

3. Data names may lose their data structure

It seems that the magic problem of the array name has been successfully solved, but the calm lake has once again set off a wave. See the following program:

1. # include <iostream. h>
2. Void arraytest (char STR [])
3 .{
4. cout <sizeof (STR) <Endl;
5 .}
6. Int main (INT argc, char * argv [])
7 .{
8. Char str1 [10] = "I love u ";
9. arraytest (str1 );
10. Return 0;
11 .}

The output result of the program is 4. Impossible?

A terrible number, as mentioned above, is the length of the pointer!

Conclusion 1: The data name is represented by an array. In the arraytest function, STR is the array name. Why is the result of sizeof pointer length? This is because:

(1) When the array name is used as a function parameter, In the function body, it loses its own meaning and is just a pointer;

(2) Unfortunately, it also loses its constant feature while losing its connotation. It can perform auto-increment, auto-subtraction, and other operations and can be modified.

Therefore, when the data name is used as a function parameter, it is completely reduced to a common pointer! Its aristocratic identity was denied and became a 4-byte civilian.

The above is conclusion 4.

  Conclusion

Finally, I once again express my deep hope that my colleagues and I will be able to think carefully about the problems in development so that we can generate a master programmer among us, top-level development books. Every time we develop books with American devils, we feel that we are too far behind.

Related Article

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.