03_Pointers and references

pointer

A pointer is a special variable used to store the address value of a variable or function.

Definition of a pointer variable
int* p; int * p; int *p; //3 definitions are the same
Operations on pointer variables

Pointers can perform assignment, addition, subtraction, and relational operations

pointer to pointer


pointers and arrays

pointer to array

The array name itself is a pointer constant , and the value of the pointer is the address value of the first element of the array

int p1[];

int* p2;

array of pointers

int* p[3]; is an array, pointers to the elements of the array


string

C-style strings:

char* cstr="abc"; Include <string.h> file, terminate character array with null character "\0". Pay attention to the problem of adding or not adding const when converting! ! !

Common string processing functions: strlen(): length strcpy(): copy strcat(): add strcmp(): compare

The string class of C++ STL:

string str="abc"; Included in <string> files, do not need to use "\0" as the terminator.

Commonly used string class functions: length(): length c_str(): conversion


quote

A reference is a C++ alias for a variable or constant identifier

Difference and connection between pointer and reference

1. Difference

(1) The pointer variable has an independent memory space to store its value, and the reference is just a symbol attached to the variable it refers to, and there is no independent storage space

(2) The pointer itself is a variable, it does not have to point to the same memory space, it can be changed to point to other spaces, and once the reference is initialized, it cannot be changed

2. Contact

Can use indirect operation means to access the space represented by a variable


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520668&siteId=291194637