Thread: How do I use this function?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    How do I use this function?

    I'm reading this wonderful book called The C Programming Language 2nd Edition and I'm having a hard time understanding how to use this function that is on page 97. Here it is:
    Code:
    int getint(int *pn)
    {
        int c, sign;
    
        while (isspace(c = getch()))
            ;
        if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
            ungetch(c);
            return 0;
        }
        sign = (c == '-') ? -1 : 1;
        if (c == '+' || c == '-')
            c = getch();
        for (*pn = 0; isdigit(c); c = getch())
            *pn = 10 * *pn + (c - '0');
        *pn *= sign;
        if (c != EOF)
            ungetch(c);
        return c;
    }
    Can someone show me an example of this function in action and also explain what is up with
    Code:
    for (*pn = 0; isdigit(c); c = getch())
            *pn = 10 * *pn + (c - '0');
    ? I just don't get these two lines. Thanks a lot.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (*pn = 0; isdigit(c); c = getch())
        *pn = 10 * *pn + (c - '0');
    *pn - This initializes the memory pointed to by the pointer pn to the value 0.

    isdigit(c) - This makes sure the character read in by the previous getch function call is a numeric character, i.e. '0' - '9'.

    *pn = 10 * *pn + (c-'0') - This multiplies the existing value stored in the location pointed to by pn by 10 and then adds the value (converted to a number, i.e. the character '2' gets converted to the integer value 2) of the character read in.

    c = getch() - This reads in the next character to begin the loop all over again.

    As an example, lets say the user enters the characters "123":

    1. The value pointed to by pn is set to 0.
    2. The value pointed to by pn is multiplied by 10 (0 * 10 is 0) and then we add 1 to it ('1' - '0' is equal to the numeric value 1) to get the final value of 1.
    3. We get the next character '2'.
    4. The value pointed to by pn is multiplied by 10 (1 * 10 is 10) and then we add 2 to it ('2' - '0' is equal to the numeric value 2) to get a final value of 12.
    5. We get the next character '3'.
    6. The value pointed to by pn is multiplied by 10 (12 * 10 is 120) and then we add 3 to it ('3' - '0' is equal to the numeric value 3) to get a final value of 123.

    Thus, the character string "123" entered by the user is converted into the numeric value 123. As far as an example goes:

    Code:
    int value;
    getint(&value);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Let's say the user enters the characters "123". When the function finishes, pn[0] will be equal to 1, pn[1] will be equal to two, and pn[3] will be equal to 3, right? In this case, all characters are valid numbers so I don't see how the above will happen when the condition in the for loop is isdigit(c). All characters are digits so the function will put all numbers in pn[0]! Am I missing something? Thanks.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    All characters are digits so the function will put all numbers in pn[0]
    Well, it will put the numbers in *pn, which is (I would guess) an int:
    Code:
    int i;
    getint(&i);
    But you're right, it could be an array:
    Code:
    int i[5];
    getint(i);
    getint(&i[3]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    "The following loop fills an array with integers by calls to getint:
    Code:
    int n, array[SIZE], getint(int *);
    
    for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
        ;
    Each call sets array[n] to the next integer found in the input and increments n."

    That's what's written on page 96 and that is what is confusing me

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Have you got it figured out now?

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    No!

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by csisz3r
    Let's say the user enters the characters "123". When the function finishes, pn[0] will be equal to 1, pn[1] will be equal to two, and pn[3] will be equal to 3, right? In this case, all characters are valid numbers so I don't see how the above will happen when the condition in the for loop is isdigit(c). All characters are digits so the function will put all numbers in pn[0]! Am I missing something? Thanks.

    >pn[0] will be equal to 1, pn[1] will be equal to two, and pn[3] will be equal to 3, right?

    Not quite. array[0] (pn[0]) will hold the value of 123. As hk_mp5kpdw demonstrated, this line:

    Code:
    *pn = 10 * *pn + (c - '0');
    ...does all the work (technically this one line does not do all the work - I am using a figure of speech.. ) - now *pn is a pointer to something, and that something is passed to the function getint() at the time it is called. The line of code above does not work on multiple elements of an array, but on one element, passed when getint() is called.

    So what does *pn point to? Consider the following, which is (almost) taken straight out of the book you are looking at:

    Code:
    int getint(int *);
    
    int main(void)
    {
             for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++);
    ...
    return 0;
    }
    As you can see from the getint() function prototype, the lone parameter is a pointer to type int - that is to say, that this functions expects to get the address of a type int variable. So this is what we have given it. As others have demonstrated, this can be done a few ways, depending on what you need.

    Now this line:

    Code:
     for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++);
    There is the passing of the address which the function requires. The first time through the loop, getint() is called, and the address passed is array[0]. The variable n is not incremented until getint() returns, and when getint() does return (provided that the user just entered some nice numbers) element 0 of array will hold that number in an int form.

    As I mentioned earlier, this line:

    Code:
    *pn = 10 * *pn + (c - '0');
    is working with just one address, which corresponds to whatever address was passed at the time of the function call. Therefore, by dereferencing the pointer as it is, the data being worked on at the time is in one location, or in this case, in just one element of the array.

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM