Thread: confused, whats the relationship between scanf and pointers

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Question confused, whats the relationship between scanf and pointers

    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?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: confused, whats the relationship between scanf and pointers

    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?
    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.

    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]

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    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 .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  3. Printing pointers with printf()
    By blacknail in forum C Programming
    Replies: 2
    Last Post: 05-25-2008, 08:15 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. pointers and functions, with scanf, please help me
    By Terrance in forum C Programming
    Replies: 2
    Last Post: 11-02-2002, 02:13 PM