A deep exploration of the difference between the C + + array names and pointers

Source: Internet
Author: User

Pointers are a feature of the C + + language, and array names are much like pointers, and even many times the array names can be used as pointers. Thus, many programmers are confused. And many university teachers, they are in the C language teaching process also wrong to teach students: "Array name is the pointer." Luckily, my college teacher is one of them. Today, I have been developing the C + + Project day after day, and I have been surrounded by programmers who keep the "array name is the pointer" misunderstanding.

Presumably the root of this misunderstanding lies in a well-known domestic C programming tutorial. If this article can correct many Chinese programmers misunderstanding the group name and the pointer, the author is not very gratified. By this article, I stand in countless knowledge of the hungry Chinese programmers, deeply hope that the domestic computer book writers, to "in-depth exploration" of the way of thinking and the earnest attitude of excellence to treat the book preparation work, I hope the market more into the author thought crystallization of painstaking efforts!

Magic Array Name

Please see the program (this program compiles under 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. The array name is not a pointer

Let's first overturn the term "array name is pointer" and use the counter-argument method.

Proving that the array name is not a pointer

Suppose: The array name is a pointer;

Then: both PSTR and STR are pointers;

Because: Under the WIN32 platform, the pointer length is 4;

So: the output of lines 6th and 7th should be 4;

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

So: The assumption is not true, the array name is not a pointer

2, the array name of the spirit of the pointer

We have shown above that the array name is really not a pointer, but let's look at the 5th line of the program. The line program assigns the array name directly to the pointer, which appears to be a pointer to the array name!

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;
0. return;
11.}

The standard C library function strcpy The two parameters that can be accepted in the original function as a char pointer, while the one 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 once again looks like a pointer!

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

The whole of a demon.

Array name

Now it's time to expose the essence of the array name, and give three conclusions:

(1) The connotation of the array name is that its reference entity is a data structure, and this data structure 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) A pointer to an array is another variable type (under the WIN32 platform, length 4), which means only the storage address of the group!

1. Array name refers to a data structure: array

It is now possible to explain why the output of line 6th of the 1th program is 10, according to conclusion 1, the connotation of the array name STR is a data structure, that is, a char array of length 10, so the result of sizeof (STR) is the memory size 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 this is the case with a C + + program:

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

We all understand that intarray is defined as an example of a data structure such as int[10], but alas, C + + does not currently support this definition.

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 array name in Program 1 is directly assigned to the pointer, and the 7th line of program 2 directly makes the array famous as a pointer parameter.

Is the following procedure set up?

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

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

pointers, whether pointers to structs, arrays, or basic data types, do not contain the connotation of the raw data structure, and the results of sizeof operations are 4 under the WIN32 platform.
And, by the way, correct another misunderstanding of many programmers. Many programmers think that sizeof is a function, but in fact, it is an operator, but its use does look much like a function. The statement sizeof (int) indicates that sizeof is really not a function, because the function accepts the parameter (a variable), and there is not a C + + function in the world that accepts a data type (such as int) as a parameter.

3, the data name may lose its data structure connotation

It seems that the magic of the array name has been resolved satisfactorily, but the calm of the lake has again set off waves. Take a look at the following procedure:

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);
0. return;
11.}

The output of the program is 4. Is it impossible?

A horrible number, already mentioned in front of it for the length of the pointer!

Conclusion 1 points out that the data name connotation is an array, in the Arraytest function body, str is the name, then why is the result of sizeof is the length of the pointer? This is because:

(1) When the array name is a function parameter, in the function body, it loses its own connotation, just a pointer;

(2) Unfortunately, in the loss of its connotation at the same time, it also lost its constant characteristics, can be self-increment, self-reduction and other operations, can be modified.

Therefore, the data name as a function parameter, its total reduced to a normal pointer! Its aristocratic identity was stripped, and became a thoroughly civilian with only 4 bytes.

The above is conclusion 4.

Conclusion

Finally, the author once again expressed deep hope that I and my fellow people can be really cautious research attitude to seriously think about the development of the problem, so as to produce in our midst master-level programmers, top development books. Every time with the development of American Devils Books, we cannot help but send the feeling: we are too far behind.

http://blog.csdn.net/adcxf/article/details/2178912

A deep exploration of the difference between the C + + array names and pointers

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.