Programming in C / C ++: Use of Pointers

With our fix ready, we can work in the same way as with a common arrangement, for example, we can modify the first element in the following way:

numbers [0] = 0;
numbers [1] = 1;
numbers [2] = 2;
numbers [3] = 3;
numbers [4] = 4;

Now, remembering what we saw earlier, let’s see the following sentence:

* numbers = 0;

With this, we are saying that the first element of the array is 0. Therefore, we could do this:

(* numbers) = 0;
numbers ++;
(* numbers) = 1;
numbers ++;
(* numbers) = 2;
numbers ++;
(* numbers) = 3;
numbers ++;
(* numbers) = 4;

But the problem now is that we do not have a pointer pointing to the first element, since the value of numbers is 110 (remember that the address of the first element is 100). Well, I know, we could easily go back and write “numbers-” a couple of times, but this might confuse us if we have a relatively long code.

Therefore, the previous form is not highly recommended (unless we create an auxiliary pointer and that pointer is the one that we increase). An equivalent way to do the above without losing the start of the fix, is this:

* numbers = 0;
* (numbers + 1) = 1;
* (numbers + 2) = 2;
* (numbers + 3) = 3;
* (numbers + 4) = 4;

To understand it, let’s see the second line:

* (numbers + 1) = 1;

We are telling the computer, that what points numbers plus 1 (that is, a direction to the right), is worth 1. It would have been wrong to write:

(* numbers + 1) = 1;

Since we would be saying that what points numbers + 1 = 1. That is, it would be like writing (assuming that the first position has the value 5): 5 + 1 = 1. Does not it make sense right?

An important point is that after reserving memory with malloc and using it (and be sure that we will not use it again), we must free it with the free function. Its use in this case would be as follows:

free (numbers);

This must be done because if we do not, the memory will remain reserved and we will be spending unnecessary memory.

As a last point regarding the utility of the pointers to be used as arrays, I want to emphasize that we can use a pointer as a string (String), to save a string we can simply write:

char * string = “Masters”;
string = “from the web”;
printf (“% s”, string);

Which does not give like exit: “of the web”. Without pointers, this would not have been possible, because if we had used fixes, we could not reassign the chain of characters string, since we would try to change the size of the array from 9 to 8 (remember that the end character of string “” also counts as one more character).

Matrices with pointers
We must think of a matrix as a pointer to a pointer. Previously we had an arrangement made with pointers, well, now we have an array of fixes (pointer to pointer). A pointer to pointer (of integer type) is declared as follows:

int ** matrix;

But remember that we must reserve the space necessary for our matrix, for this we use the malloc function:

int ** array = (int **) malloc (sizeif (int *) * 2);

With this we are creating a pointer to pointer of type int, that is, a pointer to an array of type int. But this arrangement we are aiming at has no space, we have no memory reserve, so we must also initialize it:

int ** array = (int **) malloc (sizeof (int *) * 2);
int i = 0;
for (i = 0; i <2; i ++)
array [i] = (int *) malloc (sizeof (int) * 5);

I want to emphasize the fact of having to do this, since it is a common mistake not to do it. Logic tells us that we have an arrangement of arrangements. But those fixes to which we aim, do not have space and that space is not automatically reserved, we must initialize it. So keep that in mind and do not forget it.

This done, we have a 2 × 5 matrix of integer type. To free the memory in this case, we must release each array of the array, like this:

for (i = 0; i <2; i ++)
free (matrix [i]);

This would be everything, I hope you have been clear about this issue that is very difficult to understand the first time you hear about it. As a tip, practice using pointers and so it will be much clearer.

You may also like...

1 Response

  1. 2017

    […] Previous story Programming in C / C ++: Use of Pointers […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!