Newby question about arrays

C

CeyloR

Hello everyone,

I just started with the language C and it's very interesting.

I bought the book 'The C Programming Language' from Brian W. Kernighan
and Dennis M. Ritchie.

While reading something about arrays in C, there was something not
clear to me.

In an example they use an array to count digits from some input. When
they refer to the array the code looks like:

++ndigit[c-'0'];

where c is the current character beeing read from the input (as
integer).

Am I right that the [c-'0'] stands for the convertion of the character
to it's integer value?

Any help is welcome.
 
G

gooch

++ndigit[c-'0'];
where c is the current character beeing read from the input (as
integer).

Am I right that the [c-'0'] stands for the convertion of the character
to it's integer value?

c - '0' gives the index into the array. When a character is entered by the
user, say '5', the real value of this stored in the computer is not the
integer value 5. For ASCII characters, I believe, '0' = 32 '1' = 33 and so
on. So the values are all consecutive and '5' - '0' really means 37 - 32 = 5
and that is used to index into the 6th element of the array ndigit[5]. The
value contained there is then incremented by the ++ operator.
 
W

Walter Roberson

CeyloR said:
++ndigit[c-'0'];
where c is the current character beeing read from the input (as
integer).
Am I right that the [c-'0'] stands for the convertion of the character
to it's integer value?

Not -exactly- but close.

c-'0' is certain to convert the character representation of
a digit into the numeric value represented... '0' to 0, '9' to 9
and so on.

The C language standards say almost nothing about internal
representation of characters: one of the very few things they
do promise is that the representation of the 10 digits will be
sequential and ascending starting from 0. The language standards
don't specify whether that sequence starts from 48 or from
176 or from 432493, just that they are in sequence.

The language standards do NOT make this promise for any other
characters -- the value of 'E' could be 19 less than the value of
'D' and the '$' character does not have to exist at all, for
example: only the digits are pinned down at all.


Anyhow, the [] don't have anything to do with the numeric
conversion process. The [] is the syntax for array indexing. c-'0'
produces an integral value, and ndigit[thevalue] indexes to
that location relative to the beginning of the ndigit array,
and then the ++ operator increments the value stored at that
location.
 
W

Walter Roberson

:For ASCII characters, I believe, '0' = 32 '1' = 33 and so
:eek:n.

No, in ASCII, '0' is 48. 32 is space in ASCII, then
!"#$%&'()*+,-./ all before '0'.
 
G

gooch

"> :For ASCII characters, I believe, '0' = 32 '1' = 33 and so
:eek:n.

No, in ASCII, '0' is 48. 32 is space in ASCII, then
!"#$%&'()*+,-./ all before '0'.

Sorry about that. But for the OP, the concept is still the same, just change
the values.
 
C

CeyloR

Ok that makes sense.

so in this case the c -'0' is only done to give array ndigit an index
between 0 and 9.

For example if c = 1 in ASCII it's real value is 49, the 0 in ASCII
stands for integer 48.

c-'0' then means 49-48, gives array ndigit an indez of 1.

I think I've seen the light :)
 
S

sgerchick

The read in values are ascii text, they are not numeric. by subtracting
the value of '0' you get the integer value. (also remember characters
underneath are just numbers)
 
E

Emmanuel Delahaye

gooch wrote on 03/09/05 :
++ndigit[c-'0'];
c - '0' gives the index into the array.

No. c - '0' gives the integral value of c if its value represents a
digit ('0'-'9').

[c - '0'] gives an index of some array...
When a character is entered by the
user, say '5', the real value of this stored in the computer is not the
integer value 5. For ASCII characters, I believe, '0' = 32 '1' = 33 and so
on. So the values are all consecutive and '5' - '0' really means 37 - 32 = 5
and that is used to index into the 6th element of the array ndigit[5]. The
value contained there is then incremented by the ++ operator.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
B

Barry Schwarz

Ok that makes sense.

so in this case the c -'0' is only done to give array ndigit an index
between 0 and 9.

For example if c = 1 in ASCII it's real value is 49, the 0 in ASCII
stands for integer 48.

NO! If c =1, then its real value is 1. If c = '1', then its real
value in ASCII would be 49 and in EBCDIC 241. There could be other
character encoding schemes.
c-'0' then means 49-48, gives array ndigit an indez of 1.

Only when c is '1' and you are on an ASCII system.
I think I've seen the light :)

But you must remove your sunglasses.


<<Remove the del for email>>
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top