trying to understand a couple of commands of c++

in this piece of code:
"temp = (number[++idx] - '0')"
could this be translated as:

temp=(number[idx])
idx=idx+1
but what does the -"0" mean?

  • '0'.

Convert an ASCII digit into its decimal value

No. The correct interpretation is

idx ++;
temp = (number[idx]);
temp = temp - '0';

It is likely that the value in temp will be the ascii code for a numeric digit - for example 51 for the digit 3. Subtracting the ascii code for the digit '0' (which is 48) converts the code into the number 3

...R

farolero777:
in this piece of code:
"temp = (number[++idx] - '0')"
could this be translated as:

temp=(number[idx])
idx=idx+1
but what does the -"0" mean?

++idx causes idx to be incremented before its value us used so the two are not equivalent

The - '0' (note the single quotes) subtracts the value of '0' from the value on the left. This is often used to derive the numeric value of a digit from its ASCII value. Look at an ASCII table on line to see the values involved

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.