today I decided to write this guide to deal with a very difficult topic for many: the pointers.
But without further ado, we start.
First of all, let's start with doing a little 'theory.
As you all know, its a variable declaration is organized so as to have its reference and its value. The reference of a variable is unique and represents the memory location in which it resides, so you can find it when it is invoked. The value of the variable, instead, can vary over time and represents the content of that memory cell where it resides.
In summary, then, the variable is composed of:
- Left Value (LV) of the variable reference or memory address
- Right Value (RV): contents of that cell
These value are governed by specific details. An example is the assignment: a variable assignment in the meaning of the same changes depending on the position with respect to the symbol "=".
I bring you an example so you can better understand what was said:
PHP Code:
.
.
.
int a; / * a is the name of a variable of type int * /
.
.
.
a = 1; / * Insert into the cell memory location identified by a value of 1, then we have: LV (a) <- 1 * /
a = a +3; / * Insert into the cell of a memory location identified by the value of a + 3. This means: LV (a) <- RV (a) + 3 * /
Without this parenthesis on the organization of memory variables, pass the object itself of the guide: the pointer.
The pointer is a variable that is not very different from other commonly used. Its uniqueness is that it contains the memory address of another variable, or its Left Value.
In practice, we can write this:
PHP Code:
int a = 1, y = 2, z [10];
int * ptr;
ptr = &a; / LV * (ptr) <- LV (a), ptr now points to a * /
b = * ptr; / * LV (b) <- RV (a), b is equal to the value of the variable pointed right * /
* ptr = 0; / * RV (a) <- 0 * /
ptr = & z [0] / * LV (a) <- LV (z), ptr now points to z [0] * /
- Int a = 1: is the normal declaration of a variable of type integer, the identifier
- Int * ptr: is the declaration of a variable of type "pointer" (in this case to an integer). The syntax, expressed by the BNF, which defines the declaration of a pointer is: <type> * <exp>;
- B = * ptr: the unary operator * is called indirection or deferenzazione (indirection or deferencing). In a statement specifies that what follows will be a variable pointer to the declared type. In an expression, applied to a pointer variable, allows you to access the variable pointed to by the pointer.
- Ptr = & a: is assigned to the Value of ptr Right of Left Value of a. We infer, therefore, that the unary "&" returns the value of the left variable is applied. In this case, it is said that ptr points to a.
The pointers are very useful in programming, especially in the following situations:
- Exchange of Function Parameters
- Construction of Array and more complex data structures (an example: the list)
- Dynamic memory allocation
Another important point to be clarified is how pointers can return useful as arguments to functions. Since the C function arguments are passed by value all, there are some problems difficult to solve without the use of pointers. We put our attention on this feature:
PHP Code:
void swap (int x, int y) / * WRONG * /
{
*** int tmp;
*** tmp = x;
*** x = y;
*** y = tmp;
}
The correct function makes use of the pointers:
PHP Code:
void swap (int * ptrX, int * PTRY) / * CORRECT * /
{
*** int tmp;
*** tmp = * ptrX;
*** * = * ptrX PTRY;
*** * PTRY = tmp;
}
I hope I have been clear enough in dealing with the subject and the exposition of concepts. For any additional information, criticism and advice write below using tones quiet and calm.
The next guide treat pointers, the carriers and the manner in which they are closely connected.
Greetings!






