Composite types refer to types defined based on other types.Simple variable definition: data type +declarator (variable name)ReferenceReference: gives another name to an object, and the reference type refers to another type.References are defined by writing the declarator in the form of &d, where d is the name of the declared variable.
int ival=1024;int &refVal = ival; // refVal points to ival, is another name for ivalint &refVal2; // Error: reference must be initialized
References must be initializedGenerally, when initializing a variable, the initial value is copied into the memory of the newly created object. However, for references, the program binds the reference to its initial value instead of copying the value to the reference.Once initialized, a reference remains bound to its initial value and cannot be changed.References are aliasesA reference is not an object; rather, it is just another name for an already existing object.
refVal = 2;int ii = refVal; // The result is the same as ii = ival
Assigning a value to a reference actually assigns the value to the object bound to the reference.Getting the value of a reference actually retrieves the value of the object bound to the reference.
int i = 1;int &ref = i;int &ref2 = ref; // Correct, ref2 is bound to the same variable as ref
Since a reference is not an object, you cannot define a reference to a reference.PointersA pointer itself is an object, allowing assignment and copying of pointers, and can point to different objects sequentially.Pointers do not need to be initialized at the time of definition.Pointer DefinitionThe definition of a pointer is written in the form of *d, where d is the variable name.If defining several pointer variables in one statement, each variable must have an asterisk (*) before it.
int *p1,*p2; // p1 and p2 are pointers to int typesdouble d1,*d2; // d1 is a double object, d2 is a pointer to double type
Getting the Address of an ObjectA pointer stores the address of an object. To get the address of an object, you need to use the address-of operator (&).
int ival = 1;int *p = &ival;int *p1 = p; // Correct
Pointer Values1. Points to an object2. Points to the next position of the space occupied by the neighboring object3. Null pointer, meaning it does not point to any object4. Invalid pointer, wild pointer, any value other than the above three casesAttempting to copy or access an invalid pointer will cause an error; the compiler does not check for such errors.Accessing Objects Using PointersIf a pointer points to an object, use the dereference operator (*) to access that object.
int val = 1;int *p = &val;cout << *p; // Outputs 1
The dereference operation only applies to valid pointers that actually point to an object; otherwise, it will cause an error.Null PointersA null pointer: does not point to any object. Before using a pointer, you can check if it is a null pointer.
int *p = nullptr;int *p1 = NULL;
Assignment
int i=1;int i2= 2;int *p1 = &i;int *p2 = &i2;p1 = p2; // Changes the pointer P1's target*p1 = 10; // Changes the value of the object pointed to by p1
Other Pointer Operationsif(p) operationAny non-null pointer will evaluate to true in an if statement.void* PointerA special pointer that can store the address of any object.Pointer to PointerThe level of pointers can be distinguished by the number of asterisks; ** indicates a pointer to a pointer, *** indicates a pointer to a pointer to a pointer.
int val = 1;int *p = &val;int **pp = &p;cout << val;cout << *p;cout << **p;
Reference to PointerA reference itself is not an object, so you cannot define a pointer to a reference.However, a pointer is an object, so there exists a reference to a pointer.
int i = 1;int *p;int *&r = p;
r is a reference to the pointer p.
r = &i;*r = 0;
Assigning a value to r is equivalent to assigning a value to p.