Thread: Referencing in C

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    5

    Referencing in C

    Code:
    int main()
    {
      int x;
      x = 10;
    
      printf("%d\n", x);
    
      scanf("%d", &x);
    
      printf("%d", x);
    
    }
    Why is it that I have to do a reference for &x when using
    scanf("%d", &x);
    Last edited by Salem; 3 Weeks Ago at 05:46 AM. Reason: Removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,302
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    78
    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);

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    5
    Quote Originally Posted by Dren View Post
    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);
    Thanks guys

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    78
    Quote Originally Posted by Dren View Post
    ...
    Code:
    int x = 10;
    int ptr *y = &x;
    
    scanf("%d\n", y);
    I wrote this that one day my brain was having an existential crisis. Which means it was filled with mental diarrhea instead of gray matter. Please forgive any confusion I may have caused you. I fixed the code now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Referencing and De-referencing Question
    By audinue in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 08:22 AM
  2. Self Referencing (in a way)
    By studiesrule in forum C++ Programming
    Replies: 15
    Last Post: 10-30-2006, 04:18 AM
  3. STL list and referencing
    By gautamn in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2005, 12:40 AM
  4. Help with referencing
    By daisy244 in forum C++ Programming
    Replies: 36
    Last Post: 07-29-2004, 11:57 AM
  5. Referencing Classes?
    By JaWiB in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 02:16 AM

Tags for this Thread