Pointer in C++
Pointer is a data type derived from the c language according to some sources, because it can be used on any data type. Pointers provide ease of operation on arrays and data tables. Used in dynamic address management. Since pointers point directly to the addresses, it increases the running speed of the program. It also makes it easy to linked lists, queues and stacks.
The Pointer variable stores the memory address of the variable of the type it is defined as different from other variable types. Address usage makes it easy to use very complex data structures. The memory address of variables is retrieved with the ampersand (&) sign. Variable type must be the same as pointer type. A value that holds an in whole number value cannot be stored with a pointer that holds a character value.
“&” is an operator. it has the function of showing the memory address, but it is not suitable for processing data in memory addresses. “*” operator a = 15; int * b; b = & a; declares a pointer as in equation. Also, * b = a; returns the value of & a as in equation. “* operator” in second equation is used when the transaction is to be made. In fact, the first equation is equivalent to the second equation. Therefore, & and * can be expressed as complements of each other. The * operator can be called an indirection operator, as it implicitly indicates the data in the address. Variable values outside the function block do not change, even if they are used as parameters or local variables in the call with value. If you want to change values outside the function block, the call by reference method is used. References are most used in functions. It is more efficient as it acts as a reference alias and does not copy the contents of the variable.
In fact, all variables point to an address in memory. Therefore, all variables are pointers. However, in C languages, the operations called pointer arithmetic are done. You can perform arithmetic operations on a pointer just like you do with a numeric value. There are four arithmetic operators that can be used in pointers: ‘++’, ‘- -’, ‘+’, and ‘-’. Let’s have a pointer named p. Let’s equate this pointer to a 10 integer-sized array named a. int a[10]; int *p1; p1 = a; now, thanks to this equality, the pointer p becomes equal to the value of a[0]. If we equalize p1 += 5, the pointer p1 is equal to index a[5], not index a[0]. Let’s define a p2 pointer. If we equate it to p2 = p1–3, then p2 is equal to the index a [2]. There is a strong relationship between pointers and arrays. The name of the array is a fixed pointer. the opposite is also true. If the pointer points to an array, it functions like the value of an array. For example, the equations can be continued by a[0] = * (a), a[1] = * (a + 1), a[2] = * (a + 2).
As you can see, the pointer has many uses. If you want to see more examples of pointer, you can check my GitHub account. See you in my next blog.