Programming in C / C ++: Use of Pointers

Programming in c Pointers to pass arguments by reference to functions
Suppose we have a function that makes some calculations with 2 numbers that we pass on, this is your statement:

void calculations (int first, int second)
{
first + = 10;
second + = 20;
}

Basically this function adds 10 to the first argument and 20 to the second.

We execute the following lines of code:

int first = 0, second = 0;
calculations (first, second);
printf (“First:% i, Second:% i”, first, second);

Whose output is: “First: 0, Second: 0”.

Why did not change the values ​​of our variables as we indicated? This is because in C the values ​​that we assign to our functions are passed by value, that is, we pass a copy of what we enter as an argument. This is why the value of our variables is not modified, since what we modify in the function will remain modified only in that function.

To modify the value of our variables, we must pass these variables by reference, that is, pass to the function a reference of our variable, in our case we will pass the memory address of them, that is, a pointer.

Recall that to access the memory address of a variable we wrote “& variable”, therefore, we will do this to call the calculations function:

calculations (& first, & second);

Now, what we are passing to the function is not of type integer (int), if not, of type pointer to integer (int *), therefore we must change its statement to be like this:

void calculations (int * first, int * second)
{
first + = 10;
second + = 20;
}

Now let’s analyze this:

first + = 10;

We know that first is a pointer, therefore, writing the above does not have much use for what we want to do, since we are adding 10 to the memory address and not to the value that contains that address, which is what we want. Therefore, the final function should look like this:

void calculations (int * first, int * second)
{
* first + = 10;
* second + = 20;
}

Now if we execute our code, the output it produces is: “First: 10, Second: 20”.

Arrangements with pointers
We just saw a utility of the pointers, now we will see another: Use pointers to create fixes and work with them. As we know, if we want to declare an array of 5 integers, fill it with values ​​and show on the screen, we can do it in the following way:

int numbers [5] = {0,1,2,3,4};
int i;
for (i = 0; i <5; i ++)
printf (“% i”, numbers [i]);

This program produces the following output: “0 1 2 3 4”.

Now, see the following code:

int numbers [5] = {0,1,2,3,4};
int i;
int * pointer = numbers;
for (i = 0; i <5; i ++) {
printf (“% i”, * pointer);
pointer ++;
}

If you execute it, you will see that it produces the same output. The explanation of this is that an array are only variables that are arranged in the memory side by side, as seen in the following table (which corresponds to the values ​​of our variables before entering the for loop):

We have 5 variables called numbers (I put the same name so that the idea is understood). These 5 variables arranged side by side correspond to the arrangement numbers. If we realize, pointer has the value 100, which corresponds to the first element of the array, therefore, numbers is a pointer, which points to the first element of the array that we create.

Understood this, it should make sense that in the first cycle of the for loop, the program shows us an output of 0, because it is what contains the address 100. After this, we have a line that says “pointer ++”. What this does is change what points pointer, by the direction that is next to it, that is, 110.

Now we will use pointers to reserve memory, which is the equivalent of creating an array.

The line of code:

int numbers [5];

Reserve space for 5 integers, can be written using pointers as follows:

int * numbers = (int *) malloc (sizeof (int) * 5);

[tipexperto titulo = “Nota”] To use the malloc function it is necessary to include the library “stdlib.h” in C or “cstdlib” in C ++. [/ tipexpert]

Let’s analyze this code

We have a pointer that points to an integer, nothing that we have not seen previously. After that, we have something completely unknown (or that you may have used before without having any idea what it was for), the sentence “(int *) malloc (sizeof (int) * 5);”.

Malloc is a function that reserves the amount of memory that we indicate, in this case, we are reserving 5 times the weight of an integer, whose weight is 4 bytes, you ask yourself, why do not we write directly “malloc (20)”? The answer is that an integer does not have the same weight for all the computers in which it is used. We have a function that reserves space and what returns is a pointer to the first reservation element, but the problem is that it is of the “void *” type (remember that void means that it has no type), so, we make a cast to “Int *” with the line “(int *)”.

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 !!