• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

using &

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Im confused on how you know when to use &.
for example why do they use it in this code but not the other?


 
lowercase baba
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's been a while since I did any C coding...over 15 years, at least, so I may not be 100% correct here...

I believe C is pass-by-value.  so, if you just had "scanf("%d", hourlyWage);", you'd be sending in a value 0 (assuming varibles get initialied...in C you might just get whatever junk was already in that memory space).  

Instead, you want to pass the address of what hourlyWage points to, so that inside the scanf method, it can set the value.  That way, when the funtion returns, your variable will now hold the correct value.


In your second example. you're simply setting it directly, so you don't need the ampersand.
 
Marshal
Posts: 69495
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the address operator (at least in this context; it has two other functions). If you search for scanf(), you will find it takes two parameters:-
  • 1: A format string, in this case %d
  • 2: A variable arguments list of the addresses the input is to be scanned into. Not variables, but their addresses. Such an address is obtained with &my_variable.
  • I searched for scanf() and this appears to be the best of a bad lot of results.
     
    Campbell Ritchie
    Marshal
    Posts: 69495
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    fred rosenberger wrote:. . . I believe C is pass-by-value. . . .

    It is, but it is possible to pass addresses so as to mimic pass by reference.
     
    Hanna Roberts
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh ok thanks. I think I understand it a little better now.
     
    Ranch Hand
    Posts: 179
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I searched for scanf() and this appears to be the best of a bad lot of results.


    I think the linux man(ual) pages are pretty good as reference material for C standard library functions; don't know whether you looked at those? Might be a bit dry/heavy for beginners, but it's good to know they're there.

    You can enter the command 'man scanf' if you're on a linux machine, or just google 'linux man scanf' in your browser. Eg.
    https://www.man7.org/linux/man-pages/man3/scanf.3.html
     
    Campbell Ritchie
    Marshal
    Posts: 69495
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    John Matthews wrote:. . . don't know whether you looked at those?

    No, I didn't even know there was a man page for such an instruction.

    Might be a bit dry/heavy for beginners, but it's good to know they're there. . . .

    The man pags do tend to be a bit dry, yes.

    SCANF(3)                   Linux Programmer's Manual                  SCANF(3)

    NAME
          scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf - input format conver‐
          sion

    SYNOPSIS
          #include <stdio.h>

          int scanf(const char *format, ...);
          int fscanf(FILE *stream, const char *format, ...);
          int sscanf(const char *str, const char *format, ...);

          #include <stdarg.h>

          int vscanf(const char *format, va_list ap);
          int vsscanf(const char *str, const char *format, va_list ap);
          int vfscanf(FILE *stream, const char *format, va_list ap);

      Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

          vscanf(), vsscanf(), vfscanf():
              _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L

    DESCRIPTION
          The scanf() family of functions scans input according to format as  de‐
          scribed  below.  This format may contain conversion specifications; the
          results from such conversions, . . .

    Yes, that is useful, and man scanf doesn't transfer 34653447569387 bytes of my personal information to XYZ.com for ever
     
    He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic