Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why scanf must take the address of operator

Tags:

c

scanf

addressof

As the title says, I always wonder why scanf must take the address of operator (&).

like image 817
Wazery Avatar asked Oct 08 '10 19:10

Wazery


People also ask

Why does scanf require address?

scanf requires the addressOf operator (&) because it takes a pointer as an argument. Therefore in order to pass in a variable to be set to a passed in value you have to make a pointer out of the variable so that it can be changed.

Why do we use address of operator?

Address operators commonly serve two purposes: To conduct parameter passing by reference, such as by name. To establish pointer values. Address-of operators point to the location in the memory because the value of the pointer is the memory address/location where the data item resides in memory.

Why address of operator is not written in scanf function?

The arguments of the scanf() function are the pointers types, we must provide either an address of a variable or a pointer (which contains the address of the variable). Therefore, if we are using a pointer in scanf(), we don't use address of (&) operator, because pointer contains the address itself.


6 Answers

Because C only has "pass-by-value" parameters, so to pass a 'variable' to put a value into, you have to pass its address (or a pointer to the variable).

like image 75
Michael Burr Avatar answered Oct 05 '22 14:10

Michael Burr


scanf does not take "the address of operator (&)". It takes a pointer. Most often the pointer to the output variable is gotten by using the address-of operator in the scanf call, e.g.

int i;
scanf("%i", &i);
printf("number is: %d\n", i);

But that is not the only way to do it. The following is just as valid:

int *iPtr = malloc(sizeof(int));
scanf("%i", iPtr);
printf("number is: %d\n", *iPtr);

Likewise we can do the same thing with the following code:

int i;
int *iPtr = &i;
scanf("%i", iPtr);
printf("number is: %d\n", i);
like image 45
nobody Avatar answered Oct 05 '22 14:10

nobody


Because it needs the address to place the value it reads. If you declare you variable as a pointer, the scanf will not need the &.

like image 29
Nikolai Fetissov Avatar answered Oct 05 '22 13:10

Nikolai Fetissov


Everyone else has described well that sscanf needs to put its output somewhere, but why not return it? Becuase it has to return many things - it can fill in more than one variable (driven by the formatting) and it returns an int indicating how many of those variables it filled in.

like image 25
n8wrl Avatar answered Oct 05 '22 13:10

n8wrl


When you input something with standard input device (usually keyboard), the data that comes through must be stored somewhere. You must point somewhere in the memory so that data can be stored there. To point a memory location, you need the address of that location. So you must pass your variable's address by using & operator with scanf().

like image 45
Donotalo Avatar answered Oct 05 '22 14:10

Donotalo


as the value is going to be stored,(where?), in the memory address. so scanf() deals with (&) operator.

like image 43
alfesani Avatar answered Oct 05 '22 14:10

alfesani