| Start |
|---|
| Pointer basics Note: in all the following, the existence of the C++11 constant Creating a pointer variableA pointer variable can be created using the specific The difference is clear when comparing the size of a variable with the size of a pointer to the same type: Taking the address of another variablePointers can be assigned between each other just as normal variables; in this case, it is the memory address that is copied from one pointer to another, not the actual data that a pointer points to. You can get the memory address of a variable of a given type by prefixing the variable with the address of operator In contrast with references:
Accessing the content of a pointerAs taking an address requires The combination of Dereferencing invalid pointersWhen dereferencing a pointer, you should make sure it points to valid data. Dereferencing an invalid pointer (or a null pointer) can lead to memory access violation, or to read or write garbage data. In such scenario, Hence, care must be taken when pointers are arguments of functions, as they could be null: |
| Pointer Operations There are two operators for pointers: Address-of operator (&): Returns the memory address of its operand. Contents-of (Dereference) operator(*): Returns the value of the variable located at the address specified by its operator. The asterisk (*) is used in declaring a pointer for simple purpose of indicating that it is a pointer. Don't confuse this with the dereference operator, which is used to obtain the value located at the specified address. They are simply two different things represented with the same sign. |
Pointer Arithmetic Increment / DecrementA pointer can be incremented or decremented (prefix and postfix). Incrementing a pointer advances the pointer value to the element in the array one element past the currently pointed to element. Decrementing a pointer moves it to the previous element in the array. Pointer arithmetic is not permitted if the type that the pointer points to is not complete. If a pointer to the end element is incremented, then the pointer points to one element past the end of the array. Such a pointer cannot be dereferenced, but it can be decremented. Incrementing a pointer to the one-past-the-end element in the array, or decrementing a pointer to the first element in an array yields undefined behavior. A pointer to a non-array object can be treated, for the purposes of pointer arithmetic, as though it were an array of size 1. Addition / SubtractionInteger values can be added to pointers; they act as incrementing, but by a specific number rather than by 1. Integer values can be subtracted from pointers as well, acting as pointer decrementing. As with incrementing/decrementing, the pointer must point to a complete type. Pointer DifferencingThe difference between two pointers to the same type can be computed. The two pointers must be within the same array object; otherwise undefined behavior results. Given two pointers Syntax
RemarksBe aware of problems when declaring multiple pointers on the same line. |
Read Article
https://develop.horje.com/learn/1434/reference