Re: (part 5) Han from China answers your C questions



Peter Nilsson <airia@xxxxxxxxxxx> writes:

Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
Borked Pseudo Mailed <nob...@xxxxxxxxxxxxxxxxx> writes:
a question abou "atoi" 66650...@xxxxxx said:
First,thanks for all who have answered my last question.

if                    char string[20]="12345";

how could I convert the string[2](that is "3") to an
int by using atoi? I only want to convert string[2],not
other string[i].

    int a;
    char string[20]="12345";
    atoi((a=string[2]-'0',"X"));

The consensus is

"You don't vote for kings!"
- King Arther - Monty Python's Holy Grail

that this exhibits undefined behaviour
(quite apart from being silly).

Silly, yes, ub no. Note: atoi("X") produces 0. UB occurs
if (and only if) strtol(blah, 0, 10) would produce a value
outside the range of int.

Well I agree in that that has always been my reading of the matter. I
should have said that this has been debated.

--
Ben.
.



Relevant Pages

  • Re: Plz explain me the following code
    ... int main ...   int x; ... says that when the behavior is undefined, "this International Standard ... it and executing it - "imposes no requirements" means that one of the ...
    (comp.lang.c)
  • Re: starting into H&S
    ...   int n; ...   const char *path; ... argc in that case. ... int main(int argc, char **argv) { ...
    (comp.lang.c)
  • Re: address calculation or M.S.D radix sort ?
    ...   struct node *next; ... int hash_fn; ... and "radix sort" are all very similar sorting algorithms. ... The keys are very short ...
    (comp.programming)
  • Re: Memory barrier + volatile (C++)
    ... volatile int valid = 0; ...   LeaveCriticalSection; ... declared volatile? ... for shared memory. ...
    (comp.programming.threads)
  • Re: As the Crow flies.
    ... counting primes < 1000000 ... real    0m0.090s ... mark.c:50: warning: return type defaults to ?int? ...
    (talk.origins)