when scanf is used, why does the '&' sign need to be used? does it mean that when a variable is declared it is given some location in memory and also assigned a pointer that points to it?
when scanf is used, why does the '&' sign need to be used? does it mean that when a variable is declared it is given some location in memory and also assigned a pointer that points to it?
When you define an int, a memory location is assigned to hold it. The & operator will tell the compiler to use the "address of" that int.Originally posted by temp
when scanf is used, why does the '&' sign need to be used? does it mean that when a variable is declared it is given some location in memory and also assigned a pointer that points to it?
So:
>int i;
>i = 10;
i is an int that has a value of 10. To get i's address we use
>&i
Now, scanf() requires we pass pointers to it. A pointer is a special variable type that holds the "address of" something. So, in scanf() we can use the "address of" our int, i.
>scanf("%d", &i);
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
suppose you have a variable d , scanf needs to read in value you want to put in d from stdin and then change d to the value , since we pass by reference in C , we cannot change the value of d unless we pass a pointer to d .