Board index » cppbuilder » What does this evaluate to?

What does this evaluate to?


2006-10-05 03:07:46 AM
cppbuilder9
Hi,
I'm having a problem understanding the following:
len * ( "1112481124848" [ type < 13 ? type : 0 ] - '0' )
Given that len=1 and type=3.
The part I'm not getting is the "1112481124848" string and its relation to
the ? : operator, and is it possible to subtract a character from what
should be a numerical result?
--
Regards,
IraqiGeek
www.iraqigeek.com
Nothing is rich but the inexhaustible wealth of nature. She shows us only
surfaces, but she is a million fathoms deep.
Ralph Waldo Emerson
 
 

Re:What does this evaluate to?

IraqiGeek wrote:
Quote
Hi,

I'm having a problem understanding the following:

len * ( "1112481124848" [ type < 13 ? type : 0 ] - '0' )

Given that len=1 and type=3.

The part I'm not getting is the "1112481124848" string and its relation to
the ? : operator, and is it possible to subtract a character from what
should be a numerical result?


2. The string doesn't relate to the ternary operator. The ternary
operator computes the index value, which in this case will be 3. Third
element of the string is '2'. '2' - '0' is binary value two.
.a
 

Re:What does this evaluate to?

"IraqiGeek" < XXXX@XXXXX.COM >wrote in message
Quote
I'm having a problem understanding the following:

len * ( "1112481124848" [ type < 13 ? type : 0 ] - '0' )

Given that len=1 and type=3.
Break it up into its individual operations to see what is actually
happening:
int index = (type < 13) ? type : 0;
char *str = "1112481124848";
char c = str[index];
c = c - '0';
len * c
So, when type is 3, the index into the string is 3, which is character '2',
which is then converted into numeric 2, which is then multiplied with the
len of 1.
Quote
The part I'm not getting is the "1112481124848" string and its
relation to the ? : operator
The '?:'operator is being used to calculate an index into the string to
retreive a single character from it.
Quote
is it possible to subtract a character from what should be a
numerical result?
It is not subtracting from a numeric result. It is substracting a character
from another character. It is converting the ASCII representation of a
given digit into its numeric binary value. In other words, ASCII '1'
(numeric 49) becomes numeric 1, ASCII '2' (numeric 50) becomes numeric 2,
and so on.
Gambit
 

{smallsort}

Re:What does this evaluate to?

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
Break it up into its individual operations to see what is actually
happening:

int index = (type < 13) ? type : 0;
char *str = "1112481124848";
char c = str[index];
c = c - '0';
len * c
Which is more or less the way it should be written to
begin with IMO <g>