2024-07-08
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Pointers and references are both concepts of addresses. A pointer points to a piece of memory, and its content is the address of the pointed memory; a reference is an alias for a piece of memory.
The program allocates memory area for pointer variables, but not for references.
When using a pointer, you need to add * in front of it, while a reference can be used directly.
A reference is initialized when it is defined and cannot be changed afterwards; a pointer can be changed. That is, the object of the reference cannot be changed, but the object of the pointer can be changed.
There are no null references, but there are null pointers. This makes code that uses references more efficient than code that uses pointers, because there is no need to test the validity of a reference before using it. In contrast, pointers should always be tested to prevent them from being null.
Using "sizeof" on a reference gets the size of the variable, and using "sizeof" on a pointer gets the size of the address of the variable.
Theoretically, there is no limit to the number of pointer levels, but there is only one level of reference. That is, there is no reference to a reference, but there can be a pointer to a pointer. int **p //Legal, int