(part 5) Han from China answers your C questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Borked Pseudo Mailed

    #1

    (part 5) Han from China answers your C questions

    a question abou "atoi"
    66650...@qq.com 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"));


    Yours,
    Han from China

  • Ben Bacarisse

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

    Borked Pseudo Mailed <nobody@pseudo. borked.netwrite s:
    a question abou "atoi"
    66650...@qq.com 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 that this exhibits undefined behaviour (quite apart
    from being silly). If the OP really needs to use atoi to do this
    simple conversion, the easiest way would probably be:

    a = atoi((char []){string[2], 0});

    --
    Ben.

    Comment

    • Peter Nilsson

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

      Ben Bacarisse <ben.use...@bsb .me.ukwrote:
      Borked Pseudo Mailed <nob...@pseudo. borked.netwrite s:
      a question abou "atoi" 66650...@qq.com 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.
       If the OP really needs to use atoi to do this
      simple conversion,
      The OP doesn't, so why entertain the possibility? ;)

      --
      Peter

      Comment

      • Ben Bacarisse

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

        Peter Nilsson <airia@acay.com .auwrites:
        Ben Bacarisse <ben.use...@bsb .me.ukwrote:
        >Borked Pseudo Mailed <nob...@pseudo. borked.netwrite s:
        a question abou "atoi" 66650...@qq.com 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.

        Comment

        Working...