Why don’t you need the `&`in the `scanf();`?

Asked

Viewed 1,223 times

9

I learned that the variable always has to have &, but in this example you don’t need and still show the result. Why you didn’t need the & in the scanf();?

#include <stdio.h>


int main(void)

{
  int int1, int2, int3;
  int *ptr_int = &int1;

 printf("Inteiro 1: ");
 scanf("%d", ptr_int);
 printf("Numero inserido: %d\n", int1);
 return 0;

}

2 answers

13


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!

5

The RAM (= Random access memory) of any computer is a sequence of bytes.
The position (0, 1, 2, 3, etc.) that a byte occupies in the sequence is the address (= address) of the byte. (It’s like the address of a house on a long street that has houses on one side.)

Each variable of a program occupies a certain number of consecutive bytes in the computer’s memory. A char type variable occupies 1 byte.
A variable of type int occupies 4 bytes and a double occupies 8 bytes on many computers.
The exact number of bytes of a variable is given by the sizeof operator: the expression sizeof (int), for example, gives the number of bytes of an int on your computer.

Each variable (in particular, each record and each vector) in memory has an address. On most computers, the address of a variable is the address of its first byte

The address of a variable is given by the operator &

For example, the second argument of the scanf library function is the address of the variable that should receive the value read from the keyboard

ptr_int is already a pointer to the variable (is the variable address int1)

For example, if int1 is a variable and ptr_int vale &int1 then say *ptr_int is the same as saying int1.

  • 1

    our many thanks cool and Amadeu

  • nothing, good studies!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.