Discussion on the usage comparison of array names and pointers in C + + _c language

Source: Internet
Author: User
Tags data structures strlen win32
Pointers are features of the C + + language, and array names are similar to pointers, and even many times, array names can be used as pointers. But the array name is different from the pointer in some places. Here is a summary of the difference between the array name and the usage of the pointer (some of the information comes from the Internet), and the wrong thing to do. (This program is compiled under the WIN32 platform):

1, the array name and pointer to that array, the same address, but different sizes
Use examples to illustrate:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
int arr[10]={1,1,1,1,1,1,1,1,1,1};
int* P=arr;
cout<<arr<<endl;
cout<<p<<endl;
Cout<<sizeof (arr) <<endl;//result is 40
Cout<<sizeof (p) <<endl;//result is 4
return 0;
}

Arr is the array name and P is the pointer. The 10th and 11 lines output the same value, i.e. ARR and P are the first addresses of the array. The results of line 12th and 13 are different, the size of the ARR is the size of the entire array, and the size of p is the size of the pointer.

2, the array name can be used as pointer constants, can not be self-added (+ +), self-subtraction (-), can not be modified.
We've shown that the array name is really not a pointer, but let's look at the 9th line of the program. The line program assigns the array name directly to the pointer, which appears to be an array name and indeed a pointer! We can also find examples of array names that look like pointers:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace Std;
int main (int argc, char* argv[])
{
Char str1[10] = "I love U";
Char str2[10];
Char *p = "I love U";
strcpy (STR2,STR1);
cout << "string array 1:" << str1 << Endl;
cout << "string array 2:" << str2 << Endl;
cout << strlen (str1) << "<< strlen (str2) <<" "<< strlen (p) << Endl;
return 0;
}

Program Output:
String array 1:i Love U
String array 2:i Love U
8 8 8
Standard C library function strcpy The two parameters that are acceptable in the prototype are char pointers, and we pass it to two array names in the call! The standard C library function strlen () returns the length of the start address from the argument to the first '. ' character. The array name plays the role of a pointer in these programs. On this point the array name behaves like a pointer!
But does the following code set up?
int intarray[10];
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". A pointer to an array is another type of variable (4 in length under the WIN32 platform), meaning only the address of the group

3, the array name refers to a data structure: array
int intarray[10];
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:
int[10] Intarray;
cout << sizeof (intarray);
As we all know, intarray is defined as an example of this kind of data structure, which, alas, is not currently supported by C + + int[10.

4, the data name in the function as a parameter will lose its data structure connotation
Here it seems that the array name Magic problem has been successfully resolved, but the calm of the lake but again set off waves. Take a look at the following procedure:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;
void Arraytest (char str[])
{
cout << sizeof (str) << Endl;
}
int main (int argc, char* argv[])
{
Char str1[10] = "I love U";
Arraytest (STR1);
return 0;
}

the output of the program is 4. That's impossible, right?
A dreadful number, which had previously been mentioned as the length of the pointer!
Conclusion 1 points out that the data name connotation is an array of such data structures, in the Arraytest function, str is the name of the arrays, then why sizeof the result is the length of the pointer? This is because:
(1) When an array name is used as a function parameter, in the function body, it loses its own connotation and is only a pointer;
(2) Unfortunately, in the loss of its connotation at the same time, it also lost its constant characteristics, can be used as a self-reduction operation, can be modified.
Therefore, the data name as a function parameter, its overall reduced to a normal pointer! It was stripped of its aristocratic identity and became a commoner with only 4 bytes.

5, about the difference between array name A and &a
Read the following small program, write out the output of the program:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;
int main (int argc, char* argv[])
{
int a[5] = {1,2,3,4,5};
int *P1 = a + 1;
int *P2 = (int *) (&a+1)-1;
cout << *a << "<< *p1 <<" "<< *p2 << Endl;
}

the output is: 1 2 5
Explanation: The array name represents the first address of the array, which is the value 1 of the first element in the *a, the P1 address plus 1, pointing to the second element, outputting 2, and *P2 = (int *) (&a+1)-1; A represents the first address of the array, and &a is the pointer to the array, and the &a+1 represents the address of a, which offsets an array size (here is the size of 5 int), (int *) (&a+1) points to the sixth element of the array, (int *) (&a+1)- 1 points to the fifth element of the array, so the output is 5.
It is important to distinguish between a and &a.
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.