Differences between array names and pointers

Source: Internet
Author: User

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. However, many university teachers may have to tell students wrong about the C language teaching process.

Solution: "array name is Pointer ". Fortunately, my college teacher is one of them. Today, I am working on C/C ++ projects day after day.

Developers are still filled with such programmers. They 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 Array

I am not very pleased with the misunderstanding of the name and pointer. In this article, I am deeply rooted in countless Chinese programmers who are hungry for knowledge.

It is hoped that Chinese computer book writers will be able to take the book writing work with a "in-depth exploration" way of thinking and a strict attitude towards perfection.

I hope there will be more painstaking efforts on the market for the author's thoughts!

Magic array name

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

1. # I nclude <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. In this row, the program directly assigns values to the array name.

To the pointer, it seems that the array name is indeed a pointer!

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

1. # I nclude <string. h>
2. # I nclude <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 name! 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 following conclusion:

The group name (primary) is (I .e.) Not a pointer (BIN ).

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 storage address of the array.


1. array name represents a Data Structure: Array

Now we can explain why the output of 1st rows in 6th programs is 10. According to conclusion 1, the meaning of the array name STR is a data structure.

Is a 10-character char array, so the sizeof (STR) result is the memory size occupied by this 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 understand that intarray is defined as an instance of the int [10] data structure. Unfortunately, C/C ++ currently does not support this type of setting.


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 to its 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,

However, it looks like a function. The statement sizeof (INT) indicates that sizeof is indeed not a function, because the function is connected

No C/C ++ function in the world accepts a data type (such as INT) as a "form 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. Please refer to the following section


1. # I nclude <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 meaning of Data names is the data structure of arrays. In the arraytest function, STR is the array name. Why is the conclusion of sizeof?

What is the length of the pointer? 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 is deprived and becomes a local authentic

Contains only four bytes.

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.