This paper discusses the usage of array names and Pointers in C++

  • 2020-04-02 01:01:13
  • OfStack

Pointers are a feature of the C/C++ language, and array names are so similar to Pointers that they can be used as Pointers many times. But the array name is different from the pointer in some ways. Here will array name and pointer usage of the difference to make a summary (some data from the Internet), improper place, also hope to point out! (this program is compiled on the WIN32 platform) :

1. The array name and the pointer to that array have the same address but different sizes
Use examples to illustrate:

#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;//The results of 40
 cout<<sizeof(p)<<endl;//The result of 4
 return 0;
}

Arr is the name of the array and p is the pointer. Lines 10 and 11 output the same value, that is, arr and p are the first addresses of the array. Lines 12 and 13 make a difference. The size of arr is the size of the entire array, while the size of p is the size of the pointer.

2, the array name can be used as a pointer constant, can not add (++), self-subtract (--), can not be modified.
We've shown above that array names are not Pointers, but let's look at line 9. This line assigns the array name directly to the pointer, which shows that the array name is indeed a pointer! We can also find examples of array names that look like Pointers:

#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
The standard C library function strcpy accepts two char Pointers as arguments, but we pass it two array names in the call! The standard C library function strlen() returns the length from the starting address that the argument points to to the first '\0' character. In these programs the array name ACTS as a pointer. At this point array names exhibit properties similar to Pointers!
But is the following code true?
Int intArray [10].
IntArray++;
The reader can compile it and find errors. The reason is that although the array name can be converted to a pointer to the entity it refers to, it can only be considered a pointer constant and cannot be modified. Pointers, whether to structs, arrays, or primitive data types, do not contain the contents of the original data structure, and on the WIN32 platform, the sizeof operation results in 4. By the way, correct another misunderstanding that many programmers have. Many programmers think sizeof is a function, when in fact it is an operator, but it does look too much like a function. The statement sizeof(int) indicates that sizeof is indeed not a function, because the function accepts a parameter (a variable), and no C/C++ function in the world accepts a data type (such as int) as a "parameter". The pointer to the array is of another variable type (in WIN32, length 4), which simply means the location of the array

The name of an array refers to a data structure: an array
Int intArray [10].
cout < < Sizeof (intArray);
The output of line 2 is 40 (the amount of memory occupied by an integer array).
If a C/C++ program could write:
Int [10] intArray;
cout < < Sizeof (intArray);
As we all know, intArray is defined as an instance of an int[10] data structure, which C/C++ does not currently support.

4. When a data name is used as a parameter of a function, it will lose its data structure connotation
At this point it seems that the magic problem of array names has been solved, but the calm lake waves again. Look at the following procedure:

#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. Impossible?
A terrible number, which was mentioned earlier as the length of the pointer!
Conclusion 1 points out that the data name contains the data structure of array. In the arrayTest function, STR is the array name, so why is the result of sizeof the length of the pointer? This is because:
(1) when the array name is used as a function parameter, it loses its own connotation in the function body and is just a pointer;
(2) unfortunately, in the loss of its connotation, it also lost its constant characteristics, can be done by the self - increase, self - decrease and other operations, can be modified.
Therefore, when the data name is used as a function parameter, it is reduced to an ordinary pointer! It was stripped of its aristocratic status and became a genuine plebeian with only four bytes.

5. About the difference between array names a and &a
Read the following applet and write the output:

#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, *a is the value of the first element in the array 1; P1 adds 1 to the first address, points to the second element, and outputs 2; P2 is (int *)(a+1)-1;     A represents the first address of the array, and &a is the pointer to the array, and &a+1 represents the address starting with a, offset by an array size (in this case, the size of 5 ints), (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.
The important distinction here is between a and &a.

Related articles: