1. ## one question about array

Hi there,

if there is a code like this :

Code:
```int c, ndigit[10];

if (c >= '0' && c <= '9')
++ndigit[c-'0'];```
++ndigit[c-'0'] is that the same with :

ndigit[10] = ndigit[10] + ndigit[c-'0']?

sorry if this is a stupid question, I'm just a newbie

if full program is needed (a short program this is) to answer my question, I will put it online. Just tell me so.

thanks!

2. Code:
`++ndigit[c-'0']`
Is the same as

Code:
`ndigit[c-'0'] = ndigit[c-'0'] + 1`

3. Originally Posted by vsovereign
++ndigit[c-'0'] is that the same with :

ndigit[10] = ndigit[10] + ndigit[c-'0']?
No. The ASCII value of '0' is actually 48:
ASCII Table / Extended ASCII Codes
'9' is 57. If you don't understand what the ascii table is about, ask.

So that code could be written:
Code:
```if (c >= 48 && c <= 57)
++ndigit[c-48];```
Thus c-48 will be between 0 and 9. ++ means to increment that value in the array by one. Here it is in front of the variable, meaning "pre-increment" (increment before the value is used in the statement context). Since there is no statement context, it means the same as ndigit[c-48]++.

4. Originally Posted by MK27
So that code could be written:
Code:
```if (c >= 48 && c <= 57)
++ndigit[c-48];```
"could" but very much should not! It's purely an exercize in obfuscation.

Do not do that!

5. Originally Posted by MK27
No. The ASCII value of '0' is actually 48:
ASCII Table / Extended ASCII Codes
'9' is 57. If you don't understand what the ascii table is about, ask.

So that code could be written:
Code:
```if (c >= 48 && c <= 57)
++ndigit[c-48];```
Thus c-48 will be between 0 and 9. ++ means to increment that value in the array by one. Here it is in front of the variable, meaning "pre-increment" (increment before the value is used in the statement context). Since there is no statement context, it means the same as ndigit[c-48]++.
so that means that if c = 0 -> c = 48 in ASCII

so
Code:
```ndigit[48-48] = ndigit[48-48] + 1
ndigit[0] = ndigit[0] + 1```
if we initialize ndigit[10] :

Code:
```for (i = 0; i < 10; i = i + 1)
ndigit[i] = 0;```
that means ndigit[0] = 0

so
Code:
```ndigit[0] = ndigit[0] + 1
ndigit[0] = 1```
is that correct?

6. if we initialize ndigit[10] :
Careful with your notation. ndigit[10] does not exist. ndigit is an array of 10 elements with indices 0-9 inclusive. Saying ndigit[10] is saying "the 11th entry in ndigit" which does not exist.

What you mean to say was: "if we initialize all of the entries of ndigit to 0". The [10] is not part of the variable name, it just tells you how many elements are in it.

c = 0 does not mean c=48 in ASCII. c='0' does. Note the quotes, which makes it the zero character, rather than the number 0. The character '0' has value 48 "in ASCII."

7. Originally Posted by KBriggs
Careful with your notation. ndigit[10] does not exist. ndigit is an array of 10 elements with indices 0-9 inclusive. Saying ndigit[10] is saying "the 11th entry in ndigit" which does not exist.

What you mean to say was: "if we initialize all of the entries of ndigit to 0". The [10] is not part of the variable name, it just tells you how many elements are in it.

c = 0 does not mean c=48 in ASCII. c='0' does. Note the quotes, which makes it the zero character, rather than the number 0. The character '0' has value 48 "in ASCII."
Yeah, but if we input c = 0, won't it changed to c = 48 because it's getchar() ?

so it will be
Code:
```ndigit[c-'0'] = ndigit[c-'0'] +1 --> I input c = 0

ndigit[c-'0'] = ndigit[48-48] +1
ndigit[c-'0'] = ndigit[0]+1```
I also don't understand this : does ndigit[0] + 1 = ndigit[1] ?
if yes, how come if I input 0, then it will be enterd into ndigit[1] instead of ndigit[0] ?

help will be appreciated.

8. Originally Posted by vsovereign
I also don't understand this : does ndigit[0] + 1 = ndigit[1] ?
if yes, how come if I input 0, then it will be enterd into ndigit[1] instead of ndigit[0] ?
No, you are only looking at part of the line. The whole thing translates something like:
Code:
```int x;
...
x = x + 1;```
The first part:
Code:
`ndigit[ 0 ] =`
Is the same as above when I did:
Code:
`x =`
Then the next part is the remainder:
Code:
`ndigit[ 0 ] + 1;`
Is the same as:
Code:
`x + 1;`
Again, taken all together:
Code:
```int x;
...
x = x + 1;```
Is the same as:
Code:
```int ndigit[ SOMESIZE ];
...
ndigit[ SOMEPLACE ] = ndigit[ SOMEPLACE ] + 1;```

Quzah.