Thread: K&R array example

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    K&R array example

    Hi,

    I have been attempting to understand the array example in K&R2, shown below, but I'm not familiar with the line
    Code:
     ++ndigit[c-'0'];
    I can see on running it that it increments the next element of the array when there is a non digit then a digit, but how does this for loop do this?

    Code:
    /*count digits, white space, others */
    
    #include <stdio.h>
    
    int main()
    {
        int c, i, nwhite, nother;
        int ndigit[10];
    
        nwhite = nother = 0;
        for(i=0;i<10;++i)
            ndigit[i] = 0;    //initialise array
        
        while ((c = getchar()) != EOF)
            if(c >= '0' && c <= '9')
                ++ndigit[c-'0'];
            else if (c == ' ' || c == '\n' || c == '\t')
                ++nwhite;    //increment whitespace counter
            else
                ++nother;    //increment other counter
    
        printf("digits =");
        for(i = 0;i < 10;++i)
            printf(" %d", ndigit[i]);    //print array elements
        printf(", white space = %d, other = %d\n", nwhite, nother);
    }
    Thanks
    Dave

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since characters are represented with actual numbers (ASCII chart), it stands to reason that you can subtract a number from its value and you get a position.
    It's a known solution to convert a character into its integer representative.
    For example, if c is '9', c - '0' will give you 9.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    ah, I see now - it converts char type into int to match the declaration.

    Thanks
    Dave

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by droseman View Post
    ah, I see now - it converts char type into int to match the declaration.

    Thanks
    Dave
    No, it converts char into int to make it an index into the array. And you can use ANY integer type to index into an array, char, int, long, unsigned char, unsigned int, unsigned long, enums, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Basically, it converts the inputted number (which is a char) to the corresponding integer and uses it as the index for the array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM