Introduction to C++ Pointers

A pointer (Pointer), literally understood in English is a thing pointing to a certain object, in the program it is the address pointing to the data Address.

1. Declaration of pointer

Add an asterisk between the variable type name and the variable name *. The asterisk should be as close as possible to the variable name, such as *intPtr1and *intPtr2Otherwise, it is easy to miss the asterisk in the following variable name when declaring multiple pointers. For example intPtr4, only declare An integer variable.

int *intPtr1, *intPtr2;
int* intPtr3, intPtr4;

Be sure to initialize the pointer when declaring the pointer. If it is not initialized, it may point to an unknown address.

2. Types of pointers

The type of the pointer indicates the data type stored in the address pointed by the pointer. If int *the pointer converted to float *a pointer, the program will only re-interpret the data as floating-point data.

void *A pointer just represents an address, and the data type it points to is unknown, but the data type it points to can be redefined.

3. Basic operation of the pointer

dereference*

Gets the data at the address pointed to by the pointer.
When declared, *变量名it means that the variable is used as a pointer; at other times, *变量名it means the data in the address pointed to by the pointer.

take address&

Get the memory address of the variable and assign it to the corresponding pointer

4. Arithmetic operations on pointers

Arithmetic operations of pointers and integers are different from general addition and subtraction of numbers, but are bound to pointer types. Arithmetic operations of pointers are to move forward or backward the address pointed to by the current pointer by an address of k pointer type length, and calculate The pointer after will not necessarily point to an address with valid data, so be careful when doing pointer arithmetic.

  • The array name can be regarded as a pointer to the first element of the array. Various operations of the pointer are applicable to the array name, but the array name cannot be reassigned, because the array is static, and the array name represents the only one in the current scope. Arrays, it is impossible to point to other addresses like pointers.

The subtraction operation between pointers returns the distance between pointer addresses, which is positive or negative. This distance is also bound to the pointer type.

  string str[] = {
    
     "1","2","3" };
  //取地址
  string *s1 = &str[0];
  string *s2 = &str[1];
  //解引用
  cout << s1 << ' ' << *s1 << endl;
  cout << s2 << ' ' << *s2 << endl;
  //算数操作
  cout << s1 + 1 << ' ' << *(s1 + 1) << endl;
  cout << s2 - s1 << ' ' << s1 - s2 << endl;  //1  -1

Pointers cannot be added because it is meaningless.

4. const pointer

pointer to const objectconst T *ptr1

The data in the address pointed to by ptr1 cannot be changed, but the address pointed to by itself can be changed;

const pointerT *const ptr2

The address pointed to by ptr2 cannot be modified, and the data stored in the address can be modified

const pointer to const objectconst T *const ptr3

The address pointed to by ptr3 cannot be modified, and the data in this address cannot be changed

  int numA = 3;
  //不能修改数据,能改指针的地址
  const int* ptr1 = &numA;
  //*ptr1 = 8;
  ptr1 = &numA + 1;
  
  //能修改数据,不能改指针的地址
  int* const ptr2 = &numA;
  *ptr2 = 9;
  //ptr2 = &numA + 1;
  
  //不能修改数据,也不能改指针的地址
  const int* const ptr3 = &numA;
  //*ptr3 = 9;
  //ptr3= &numA + 1;

5. Pointer array and array pointer

  • Pointer array : T *ptrArr[size], ptrArr is an array, each element in the array is a pointer, the pointer type is ** T*, you can use*(ptrArr[i]) ** to read the elements of the array;

  • Array pointer : T (*arrPtr)[size], arrPtr points to an array, you need to assign the address of the array to arrPtr, after dereferencing it is equivalent to an array, you can use ** (*arrPtr)[i]** to read the elements of the array.

int num[5] = {
    
     1,2,3,4,5 };
//指针数组:元素是指针的数组
int* ptrArr[5] = {
    
     &num[0],&num[1],&num[2],&num[3],&num[4] };
for (int i = 0; i < 5; ++i) {
    
    
  cout << ptrArr[i] << ':' << *(ptrArr[i]) << ' ';
}; cout << endl;

//数组指针:指向数组的指针
int(*arrPtr)[5] = &num;
for (int i = 0; i < 5; ++i) {
    
    
  cout << ptrArr[i] << ':' << (*ptrArr)[i] << ' ';
}; cout << endl;

6. Pointer to Pointer

A pointer can point to any variable or object, and of course it can point to a pointer.
When a pointer to a pointer is declared, ** is used to represent the pointer type pointed to by the pointer. The pointer of the pointer is generally used to modify the incoming pointer when the function passes parameters.

const_cast和reinterpret_cast

  • const_cast : Modify the const or volatile attribute of the type, and return a pointer or reference to a non-const object. At this time, its data can be changed through the pointer or reference.
    int numA = 3;
    const int* ptr1 = &numA;
    int* ptr2 = const_cast<int*>(ptr1);
    
    const int numB = 3;
    int& numb = const_cast<int&>(numB);
    
  • reinterpret_cast : change the data type of the variable

7. Some applications of pointers

Sort two arrays using pointers


void sortPtr(int rNum, int* a, int aNum, int* b, int bNum) {
    
    
  int* p, * q;
  int* r = (int*)malloc((aNum + bNum) * sizeof(int));
  int* res = r;

  for (p = a, q = b; p < a + aNum && q < b + bNum;) {
    
    
    if (*p > *q) {
    
    
      *r++ = *q++;
    }
    else {
    
    
      *r++ = *p++;
    }
  }
  if (p < a + aNum) {
    
    
    for (; p < a + aNum;)
      *r++ = *p++;
  }
  else if (q < b + bNum) {
    
    
    for (; q < b + bNum;)
      *r++ = *q++;
  }
  for (r = res; r < res + aNum + bNum; ++r) {
    
    
    cout << *r << ' ';
  }
  cout << endl;
}

Iterate over arrays using pointers

//遍历数组时,要知道该数组的长度,以免指针超出数组的范围,导致读取数据失败
void testStr(string* p, int size) {
    
    
  for (int i = 0; i < size; ++i) {
    
    
    cout << p << endl;
    ++p;
  }
}

//遍历字符数组时,
void testChar(const char* p) {
    
    
  while (*p) {
    
    
    cout << *p << endl;
    ++p;
  }
}

Guess you like

Origin blog.csdn.net/qq_33021529/article/details/127501745