Always :) that you learn that something "always has to be" has something wrong in the learning or in the mechanism, because if you have to put something to get a result, but it may not, it is because in some situation you do not need, otherwise should not be required to wear something that has to always wear like this in any case.
Passing of argument by reference
You have to understand why there is a pointer. And that it is used in a function parameter as a way to make it bidirectional, that is, it causes you to send a value to the function, but if it is changed the variable that contained it in the calling function will have its value changed together.
That’s what happens in scanf()
. You are passing a variable to the function not because you need to send a value to the function, in fact the variable may not even have a valid value, for the scanf()
it does not matter, the issue is that at the end of the implementation of the scanf()
the value typed by the user needs to be placed somewhere and he puts it precisely in the variable you passed as the second argument (the first is the formatting text, and there may be other arguments with more variables).
So that the variable in the calling function receives the value of the scanf()
it needs to be passed as a reference, ie it has to be a pointer to a region of the variable’s memory (if you still have doubt understand what is a variable).
You might think, "but why don’t you return that value?". Because function already has a return to something else, something that people don’t realize and almost always use incorrectly. Almost all use of scanf()
should be in a if
to indicate if the operation was successful, because that is what returns. You should not use the variable that receives its value without a check to see if everything went right. Read the documentation and see what is the return of scanf()
.
See more in Why use pointers as function parameters?.
Because you don’t need in the example
So understand that the most common when learning to use the scanf()
is to do something like this:
int x;
scanf("%d", &x);
This is necessary because you are passing the variable address x
for function scanf()
, is not passing the value of x
. To pass the memory address an operation is required, since the default is to take the value of the variable. That operator is the &
.
But there are some cases you don’t need. Why? Because the variable is already a pointer. If the parameter of scanf()
expects a pointer and its variable already stores a memory address, there is no reason to use the &
. A well-known case is:
char texto[11];
scanf("%s", texto);
texto
is already a pointer, no need. Your case is the same thing, ptr_int
is a pointer, so you don’t need to use any operator to pick up memory address, it is already what you need, even because it has already been used in (strictly speaking, needlessly, only do this in didactic exercise):
int *ptr_int = &int1;
I put in the Github for future reference.
I liked the answer, it helped a lot!
– Maurício Z.B