a question abou "atoi"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 66650755@qq.com

    a question abou "atoi"

    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].

    I've written these sentences:

    int a;
    char string[7]="111111";

    a=atoi(string[3]);

    however,the compiler said:
    error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
    char *'
    Conversion from integral type to pointer type requires
    reinterpret_cas t, C-style cast or function-style cast.



    I'm eager to find an solution.thinks ,thinks,thinks! !
  • Keith Thompson

    #2
    Re: a question abou "atoi&quot ;

    66650755@qq.com writes:
    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].
    atoi is dangerous; avoid it. It can invoke undefined behavior on
    error, i.e., arbitrarily bad things can happen. We had a discussion
    here recently about what kinds of errors, overflow vs. ill-formed
    strings, can cause what consequences, but the bottom line is that it
    can't be used safely unless you carefully check the argument first.
    strtol() can be a bit harder to use, but it's much easier to use
    safely.
    I've written these sentences:
    C doesn't have "sentences" .
    int a;
    char string[7]="111111";
    >
    a=atoi(string[3]);
    >
    however,the compiler said:
    error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
    char *'
    Conversion from integral type to pointer type requires
    reinterpret_cas t, C-style cast or function-style cast.
    The error message implies that you're using a C++ compiler. Decide
    which language you want to use, and post to the appropriate group.
    Most C++ compilers can be invoked as C compilers; if you want to write
    C, find out how to do this for yours. (Sometimes naming your source
    file with a ".c" suffix is sufficient.)

    A string is a sequence of characters, terminated by and including a
    terminating null character ('\0'). A single character isn't a string
    (unless it's a '\0', but that's not useful here). atoi() expects a
    pointer to a string; passing it a single character doesn't make sense.
    Ignore what it says about converting to a pointer type; that's not
    what you want to do.

    If you really want to extract a single character from a string and
    pass it *as a string* to atoi (or, preferably, to strtol), you could
    declare a 2-character array, copy the desired character to the first
    element, and set the second element to '\0'. The contents of the
    array are now a valid string, and you can pass it (or, rather, a
    pointer to it) to atoi or strtol.

    But there's an easier way. Since the language guarantees that, for
    whatever character set you're using, the digits '0' through '9' have
    contiguous representations , the following are guaranteed:

    '0' - '0' == 0
    '1' - '0' == 1
    '2' - '0' == 2
    ...
    '9' - '0' == 9

    The value of '0' is most likely 48 on your system, or it might be 240
    if you're using an IBM mainframe, but the above are still guaranteed.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • CBFalconer

      #3
      Re: a question abou &quot;atoi&quot ;

      66650755@qq.com wrote:
      >
      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 charval;
      ...
      charval = string[2] - '0';

      all done.

      --
      [mail]: Chuck F (cbfalconer at maineline dot net)
      [page]: <http://cbfalconer.home .att.net>
      Try the download section.

      Comment

      • user923005

        #4
        Re: a question abou &quot;atoi&quot ;

        On Oct 30, 5:17 pm, 66650...@qq.com wrote:
        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].
        >
        I've written these sentences:
        >
                int a;
                char string[7]="111111";
        >
                a=atoi(string[3]);
        >
        however,the compiler said:
        error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
        char *'
                Conversion from integral type to pointer type requires
        reinterpret_cas t, C-style cast or function-style cast.
        #include <string.h>
        #include <stdio.h>

        int main(void)
        {
        int a;
        char string[7] = "123456";
        char substring[2] = {0}; /* now contains [0][0] */
        int index;

        for (index = 0; index < strlen(string); index++) {
        substring[0] = string[index];
        a = atoi(substring) ;
        printf("%d\n", a);
        }
        return 0;
        }

        Comment

        • Mark L Pappin

          #5
          Re: a question abou &quot;atoi&quot ;

          66650755@qq.com writes:
          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].
          'string[2]' is not a string; it is a single character.
          The array named 'string' contains a string, initialized as if by

          char string[20] = {'1', '2', '3', '4', '5', '\0',
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

          Note: you should not use 'string', or any other identifier beginning
          with 'str', as the name of any object or function you define, because
          almost all such names are reserved for the use of the implementor,
          which you are not.

          A string consists of a sequence of zero or more characters followed by
          a null character. You need to pass a pointer to such a sequence to
          'atoi()'.

          Note also: you should probably not use 'atoi()' in any case as it has
          a design limitation which has been discussed here recently; use
          'strtol()' instead, as it can report its results in more detail.
          I'm eager to find an solution.thinks ,thinks,thinks! !
          To extract the decimal value of a single digit from within a string
          such as that shown above:

          int value = string[2] - '0';

          'value' now contains the value 3. This works because the Standard
          guarantees that the representations of the characters '0' through '9'
          are sequential. It makes no such guarantee about non-digits, so you
          can not portably assume that

          ('a' + 2) == 'c'

          for example.

          mlp

          Comment

          • 66650755@qq.com

            #6
            Re: a question abou &quot;atoi&quot ;

            On 10ÔÂ31ÈÕ, ÉÏÎç9ʱ32·Ö, Keith Thompson <ks...@mib..org wrote:
            66650...@qq.com writes:
            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].
            >
            atoi is dangerous; avoid it. It can invoke undefined behavior on
            error, i.e., arbitrarily bad things can happen. We had a discussion
            here recently about what kinds of errors, overflow vs. ill-formed
            strings, can cause what consequences, but the bottom line is that it
            can't be used safely unless you carefully check the argument first.
            strtol() can be a bit harder to use, but it's much easier to use
            safely.
            >
            I've written these sentences:
            >
            C doesn't have "sentences" .
            >
            int a;
            char string[7]="111111";
            >
            a=atoi(string[3]);
            >
            however,the compiler said:
            error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
            char *'
            Conversion from integral type to pointer type requires
            reinterpret_cas t, C-style cast or function-style cast.
            >
            The error message implies that you're using a C++ compiler. Decide
            which language you want to use, and post to the appropriate group.
            Most C++ compilers can be invoked as C compilers; if you want to write
            C, find out how to do this for yours. (Sometimes naming your source
            file with a ".c" suffix is sufficient.)
            >
            A string is a sequence of characters, terminated by and including a
            terminating null character ('\0'). A single character isn't a string
            (unless it's a '\0', but that's not useful here). atoi() expects a
            pointer to a string; passing it a single character doesn't make sense.
            Ignore what it says about converting to a pointer type; that's not
            what you want to do.
            >
            If you really want to extract a single character from a string and
            pass it *as a string* to atoi (or, preferably, to strtol), you could
            declare a 2-character array, copy the desired character to the first
            element, and set the second element to '\0'. The contents of the
            array are now a valid string, and you can pass it (or, rather, a
            pointer to it) to atoi or strtol.
            >
            But there's an easier way. Since the language guarantees that, for
            whatever character set you're using, the digits '0' through '9' have
            contiguous representations , the following are guaranteed:
            >
            '0' - '0' == 0
            '1' - '0' == 1
            '2' - '0' == 2
            ...
            '9' - '0' == 9
            >
            The value of '0' is most likely 48 on your system, or it might be 240
            if you're using an IBM mainframe, but the above are still guaranteed.
            >
            --
            Keith Thompson (The_Other_Keit h) ks...@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"


            Thank you very much for your replay,and I've learnt lot from it.

            Comment

            • William Pursell

              #7
              Re: a question abou &quot;atoi&quot ;

              On 31 Oct, 02:05, user923005 <dcor...@connx. comwrote:
                  for (index = 0; index < strlen(string); index++) {
                      substring[0] = string[index];
                      a = atoi(substring) ;
                      printf("%d\n", a);
                  }
              Don't put strlen as a loop condition. Try:

              len = strlen( string );
              for( i = 0 ; i < len; i++ )
              ....

              Comment

              • Richard Heathfield

                #8
                Re: a question abou &quot;atoi&quot ;

                William Pursell said:
                On 31 Oct, 02:05, user923005 <dcor...@connx. comwrote:
                >
                >for (index = 0; index < strlen(string); index++) {
                >substring[0] = string[index];
                >a = atoi(substring) ;
                >printf("%d\n ", a);
                >}
                >
                Don't put strlen as a loop condition. Try:
                >
                len = strlen( string );
                for( i = 0 ; i < len; i++ )
                ...
                Probably best to explain why. Even though a skilled implementor of the
                strlen function may well be able to use some clever tricks to speed it up
                on a particular platform, its execution time is nevertheless going to be
                approximately proportional to the length of the string, even for fairly
                short strings, and *at the point of the call* a compiler will typically be
                unable to prove that the string is not modified inside the loop, so it
                won't be justified in caching the value. Thus, it will be called on every
                single iteration of the loop, leading to O(n*n) behaviour - quadratic
                performance, i.e. fairly dire. But *you* know the string is not modified
                inside the loop, so you *are* justified in caching the value - and should
                do so, as it reduces quadratic to linear.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • Mark McIntyre

                  #9
                  Re: a question abou &quot;atoi&quot ;

                  66650755@qq.com wrote:
                  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?
                  You don't need to - string[2] is already an integer type.

                  int x = string[2];

                  does the trick.
                  int a;
                  char string[7]="111111";
                  >
                  a=atoi(string[3]);
                  atoi takes a string, string[2] is a character - which is an integer type.
                  error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
                  char *'
                  Conversion from integral type to pointer type requires
                  reinterpret_cas t, C-style cast or function-style cast.
                  You're using a C++ compiler, don't do that if you're compiling C, the
                  two languages are different.

                  Comment

                  • Kelsey Bjarnason

                    #10
                    Re: a question abou &quot;atoi&quot ;

                    On Fri, 31 Oct 2008 11:23:23 +0000, Mark McIntyre wrote:
                    66650755@qq.com wrote:
                    >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?
                    >
                    You don't need to - string[2] is already an integer type.
                    >
                    int x = string[2];
                    >
                    does the trick.
                    True, but the value I get here when doing this is 51; I suspect he wanted
                    the value 3.

                    int x = string[2] - '0'; would do the trick.

                    Comment

                    • Mark McIntyre

                      #11
                      Re: a question abou &quot;atoi&quot ;

                      Kelsey Bjarnason wrote:
                      On Fri, 31 Oct 2008 11:23:23 +0000, Mark McIntyre wrote:
                      >
                      >66650755@qq.com wrote:
                      >>First,thank s 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?
                      >You don't need to - string[2] is already an integer type.
                      >>
                      >int x = string[2];
                      >>
                      >does the trick.
                      >
                      True, but the value I get here when doing this is 51; I suspect he wanted
                      the value 3.
                      >
                      int x = string[2] - '0'; would do the trick.
                      I was kinda leaving that as an exercise for the reader, given that its
                      an FAQ... :-)

                      --
                      Mark McIntyre

                      CLC FAQ <http://c-faq.com/>
                      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                      Comment

                      Working...