This problem won't let me sleep (could you give me a tip to solve it, please):
Write a program that reads two values of type float, and then performs the sum of the two, but without directly using a script of the form a + b, but instead uses these pointers.
Pointers have nothing to do with hexadecimal arithmetic as you suggest in a comment. The only relationship is that to print the value of a pointer, the format string is usually used
"%p"
, which dumps that value in hexadecimal. But in reality the value of the pointer is just a number, regardless of the format in which it is printed.That number represents the memory address where another variable is located. The pointer is then said to point to that memory address.
To work with pointers you have to take into account three phases:
Statement
It's as simple as putting a
*
in front of the name of the variable you're declaring. Thus, if you want a pointer that points to data of type float, just put:initialization
Like any other variable, the fact of declaring it does not give it an initial value (although some compilers give the value 0 to uninitialized variables, it is not good practice to assume that this is always the case since some compilers do not).
If you haven't initialized it, it's possible that the pointer contains a random number. You can check it by printing its value with
printf("%p\n", mi_puntero)
. If you see "(nil)" it is initialized to 0, but if you see any other hexadecimal number it is not initialized and therefore points to a random address.An uninitialized pointer should not be used, as the effects are unpredictable (the program will most likely crash and stop executing, but worse things can happen). So how to initialize a pointer?
There are two correct ways and one incorrect way (which I comment on anyway so you can see):
Give it a value directly, with something like
mi_puntero = 5000
. This is the wrong way because you're making the pointer point directly to an address of your choosing, in this case 5000, but you don't know what's at that address so it's dangerous to try to use it. Most likely, that address is protected by the operative and trying to use it will cause an error.Give it the address where another variable is located. For example, suppose you have a variable (which has to be of type
float
in this case) calledb
. The address of that variable is obtained with the operator&
so you can do:In this case the pointer points to the address where it is
b
, so it is usually said directly that the pointer points tob
Calling
malloc()
(this is called dynamic memory allocation). I am not going to go into details of this method because it goes beyond the objectives of the question. But you should know that it is the way to "create" new variables while the program is running, without having to declare them previously. These "variables" have no names, only addresses where they are found, and therefore the only way to work with them is through pointers.use the pointer
Once you have the pointer pointing to an address, you can manipulate the data at that address by putting the operator
*
in front of the pointer.Yes, the operator
*
is used for several things in C. In addition to being the multiplication operator, it is also, as we saw before, the one that declares that a variable is of type pointer, and we now see that it is also the one that allows access to the pointed value.So, since we had to
mi_puntero
point tob
, we'll need to*mi_puntero
access the value ofb
. We can access that value only to query it, for example to print it:Notice how when putting
mi_puntero
what we query is the value inside the pointer (and I print it with"%p"
) while when putting*mi_puntero
what we query is what is at the address the pointer points to. In this case we would observe the value 3.1416, since that is the value ofb
.We can not only query that value, but also change it , if we use
*mi_puntero
the left side of an assignment, for example like this:in this way we change what is in the direction to which it points. Watch out, since the pointer was pointing to
b
, we just changed the value ofb
!This is the basics you need to know about pointers to answer the exercise.
Solution
I'm not going to give you the code, so you can try it yourself, but I understand that the idea of the exercise would be:
float
, saya
andb
.a
and the other tob
.a+b
, but without using the variablesa
andb
, using the pointers instead. Remember to put a*
in front of each pointer to access the value pointed to.