Why is it that I have to do a reference for &x when usingCode:int main() { int x; x = 10; printf("%d\n", x); scanf("%d", &x); printf("%d", x); }
scanf("%d", &x);
Why is it that I have to do a reference for &x when usingCode:int main() { int x; x = 10; printf("%d\n", x); scanf("%d", &x); printf("%d", x); }
scanf("%d", &x);
Last edited by Salem; 01-20-2019 at 05:46 AM. Reason: Removed crayola
You need to pass a pointer in order for scanf to modify the object through the pointer.
By the way, post your code as plain text without special markup other than code tags. The code tags will trigger its own syntax highlighting and line numbering.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There are no references in c.
scanf takes a const pointer to char as it's first argument, so you must either give it a pointer to a char or the address of a char, which for all intents and purposes, is the same thing.
You obtain the address of a variable with the & operator. Which is what you are doing there.
Another way would be.
Code:int x = 10; int_ptr y = &x; scanf("%d\n", y);