integer expression in a subscript

G

Gus Tabares

Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

.... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.

Thanks,
Gus
 
E

E. Robert Tisdale

Gus said:
I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
subtraction is needed here? Why couldn't it just be ++ndigit[c]?
I may be missing something totally here.

> cat main.c
#include<stdio.h>

int main(int argc, char* argv[]) {
int c = '0';
fprintf(stdout, "%d = c\n", c);
return 0;
}
> gcc -Wall -std=c99 -pedantic -O2 -o main main.c
> ./main
48 = c
 
B

Barry Schwarz

Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.

The subtraction has the affect of converting the value in c which is
the character representation of a digit (in ASCII, '0' is 0x30 or 48)
to the numeric value of the digit. This is guaranteed to work because
the standard requires the representation of the 10 digits to be
sequential in the normal order.

Thus, if you type a two, c will be set to '2' and the statement in
question will increment the value of ndigit[2] by 1. Without the
subtraction, it would attempt to increment the value of ndigit[50] (or
ndigit[242] if your machine is EBCDIC) which doesn't exist.


<<Remove the del for email>>
 
P

pete

Gus said:
Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.


(c) is equal to some number from '0' to '9', inclusive,
at that point in the code, which implies that
(c - '0') is equal to some number from 0 to 9, inclusive,
at that point in the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top