Thread: & and no &

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    & and no &

    I dont know why. sometime, when performing scanf it require & while sometime(in string), it dont.

    printf("%d",&num1); <---------------no string

    printf("%s",num1); <----------------string

    ty.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    You should (almost) never use & with printf, as it takes the value of its arguments, not a pointer (except when using strings...). You should (almost) always use & with scanf as it need to write to the arguments passed to the function.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Quote Originally Posted by iceaway View Post
    You should (almost) never use & with printf, as it takes the value of its arguments, not a pointer (except when using strings...). You should (almost) always use & with scanf as it need to write to the arguments passed to the function.
    NONNO. I mistype. It should be scanf!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ncode View Post
    NONNO. I mistype. It should be scanf!
    You need the & with ints and floats etc. because these are values and you need to hand it an address.
    You don't need it with arrays and strings because C treats these as pointers to begin with.

Popular pages Recent additions subscribe to a feed