Problem with atoi()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tolkien

    #1

    Problem with atoi()

    Hi,My problem is this:
    I have :
    char matrix[3][3]=....
    int x;
    x=atoi(matrix[2][2]);

    This doesn't work.Any help?

    I don't have any errors.Just a warning" passing argument 1 of 'atoi'
    makes pointer from integer without a cast"

  • Army1987

    #2
    Re: Problem with atoi()

    On Sun, 09 Sep 2007 12:30:47 +0000, tolkien wrote:
    Hi,My problem is this:
    I have :
    char matrix[3][3]=....
    int x;
    x=atoi(matrix[2][2]);
    >
    This doesn't work.Any help?
    atoi takes a pointer to char. matrix[2][2] is a char.
    What are you trying to do? I'd be tempted to say "maybe you mean
    atoi(&matrix[2][2])", but matrix[2][2] is the last byte in the
    object you defined, so it can't be the beginning of a nonempty
    string.

    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen

    Comment

    • Joe Wright

      #3
      Re: Problem with atoi()

      tolkien wrote:
      Hi,My problem is this:
      I have :
      char matrix[3][3]=....
      int x;
      x=atoi(matrix[2][2]);
      >
      This doesn't work.Any help?
      >
      I don't have any errors.Just a warning" passing argument 1 of 'atoi'
      makes pointer from integer without a cast"
      >
      Here's a clue. The prototype:

      int atoi(const char *_s);

      Note matrix[2][2] is type char, not char*.

      --
      Joe Wright
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • tolkien

        #4
        Re: Problem with atoi()

        i have this matrix:
        char matrix[3][3]={'3','4','5',
        '6','7','8',
        '1','2','3' };

        and i want to use one of its elements as integer .
        for example int x= matrix[1][2] (8)

        Comment

        • runner

          #5
          Re: Problem with atoi()


          "tolkien" <gvovos@gmail.c omwrote in message
          news:1189348864 .972166.161850@ 22g2000hsm.goog legroups.com...
          i have this matrix:
          char matrix[3][3]={'3','4','5',
          '6','7','8',
          '1','2','3' };
          >
          and i want to use one of its elements as integer .
          for example int x= matrix[1][2] (8)
          >
          In this case, "atoi" simply becomes

          int v = matrix[i][j] - '0';

          but if you have to deal with multi-digit integers
          you'd better declare the matrix as char *matrix[3][3]
          and assign to its elements the pointers to strings
          allocated elsewhere. Then you can use atoi.



          Comment

          • tolkien

            #6
            Re: Problem with atoi()

            thank you very much!!!!!

            Comment

            • Malcolm McLean

              #7
              Re: Problem with atoi()


              "tolkien" <gvovos@gmail.c omwrote in message
              news:1189348864 .972166.161850@ 22g2000hsm.goog legroups.com...
              >i have this matrix:
              char matrix[3][3]={'3','4','5',
              '6','7','8',
              '1','2','3' };
              >
              and i want to use one of its elements as integer .
              for example int x= matrix[1][2] (8)
              >
              atoi() works on strings, not single characters

              Write a function

              int chartoval(char ch)
              {
              int answer = ch - '0';
              assert(answer >= 0 && answer <= 9);
              return answer;
              }

              this works because you are guaranteed consecutive codes for decimal digits.
              The assert() is for safety.

              --
              Free games and programming goodies.


              Comment

              • Keith Thompson

                #8
                Re: Problem with atoi()

                "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                "tolkien" <gvovos@gmail.c omwrote in message
                news:1189348864 .972166.161850@ 22g2000hsm.goog legroups.com...
                >>i have this matrix:
                > char matrix[3][3]={'3','4','5',
                > '6','7','8',
                > '1','2','3' };
                >>
                >and i want to use one of its elements as integer .
                >for example int x= matrix[1][2] (8)
                >>
                atoi() works on strings, not single characters
                >
                Write a function
                >
                int chartoval(char ch)
                {
                int answer = ch - '0';
                assert(answer >= 0 && answer <= 9);
                return answer;
                }
                >
                this works because you are guaranteed consecutive codes for decimal digits.
                The assert() is for safety.
                Using assert() is appropriate only if a non-digit character in the
                matrix is a program bug. If it could be the result of, for example,
                invalid input, assert() is not the way to check for the error.

                The code snippet we've seen is clearly a toy example; we don't have
                enough information about how the matrix is really initialized (unless
                this is a homework problem).

                I'd also check *before* converting the character to an int, probably
                using isdigit().

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

                Comment

                • Army1987

                  #9
                  Re: Problem with atoi()

                  On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
                  "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                  >int chartoval(char ch)
                  >{
                  > int answer = ch - '0';
                  > assert(answer >= 0 && answer <= 9);
                  > return answer;
                  >}
                  [snip]
                  I'd also check *before* converting the character to an int, probably
                  using isdigit().
                  isdigit() could return true for locale-specific digits, which
                  could be distinct from the '0' - '9' Western Arabic numerals in
                  the basic character set, for which the ch - '0' trick doesn't
                  work.
                  --
                  Army1987 (Replace "NOSPAM" with "email")
                  If you're sending e-mail from a Windows machine, turn off Microsoft's
                  stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                  characters through your mail. -- Eric S. Raymond and Rick Moen

                  Comment

                  • Keith Thompson

                    #10
                    Re: Problem with atoi()

                    Army1987 <army1987@NOSPA M.itwrites:
                    On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
                    >"Malcolm McLean" <regniztar@btin ternet.comwrite s:
                    >>int chartoval(char ch)
                    >>{
                    >> int answer = ch - '0';
                    >> assert(answer >= 0 && answer <= 9);
                    >> return answer;
                    >>}
                    [snip]
                    >I'd also check *before* converting the character to an int, probably
                    >using isdigit().
                    isdigit() could return true for locale-specific digits, which
                    could be distinct from the '0' - '9' Western Arabic numerals in
                    the basic character set, for which the ch - '0' trick doesn't
                    work.
                    No, isdigit() only returns true for '0' .. '9'. Quoting C99
                    7.4.1.5p2:

                    The isdigit function tests for any decimal-digit character (as
                    defined in 5.2.1).

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

                    Comment

                    • Charlie Gordon

                      #11
                      Re: Problem with atoi()

                      "Army1987" <army1987@NOSPA M.ita écrit dans le message de news:
                      pan.2007.09.10. 09.26.24.876899 @NOSPAM.it...
                      On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
                      >"Malcolm McLean" <regniztar@btin ternet.comwrite s:
                      >>int chartoval(char ch)
                      >>{
                      >> int answer = ch - '0';
                      >> assert(answer >= 0 && answer <= 9);
                      >> return answer;
                      >>}
                      [snip]
                      >I'd also check *before* converting the character to an int, probably
                      >using isdigit().
                      isdigit() could return true for locale-specific digits, which
                      could be distinct from the '0' - '9' Western Arabic numerals in
                      the basic character set, for which the ch - '0' trick doesn't
                      work.
                      Really ? I don't think so:

                      7.4.1.5p2: The isdigit function tests for any decimal-digit character (as
                      defined in 5.2.1).

                      5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

                      isxxx functions that have locale specific behaviour are noted as such.
                      isdigit and iscntrl do not seem to have locale specific behaviour.

                      I cross post to comp.std.c to get an authoritative answer on this question
                      relating to the C Standard.

                      --
                      Chqrlie.


                      Comment

                      • pete

                        #12
                        Re: Problem with atoi()

                        Malcolm McLean wrote:
                        >
                        "tolkien" <gvovos@gmail.c omwrote in message
                        news:1189348864 .972166.161850@ 22g2000hsm.goog legroups.com...
                        i have this matrix:
                        char matrix[3][3]={'3','4','5',
                        '6','7','8',
                        '1','2','3' };

                        and i want to use one of its elements as integer .
                        for example int x= matrix[1][2] (8)
                        atoi() works on strings, not single characters
                        >
                        Write a function
                        >
                        int chartoval(char ch)
                        {
                        int answer = ch - '0';
                        assert(answer >= 0 && answer <= 9);
                        return answer;
                        }
                        >
                        this works because you are guaranteed consecutive
                        codes for decimal digits.
                        The assert() is for safety.
                        This way also protects you in the unlikely event
                        that ch equals INT_MIN:

                        int chartoval(char ch)
                        {
                        assert(ch >= '0' && ch <= '9');
                        return ch - '0';
                        }

                        --
                        pete

                        Comment

                        • pete

                          #13
                          Re: Problem with atoi()

                          Charlie Gordon wrote:
                          >
                          "Army1987" <army1987@NOSPA M.ita écrit dans le message de news:
                          pan.2007.09.10. 09.26.24.876899 @NOSPAM.it...
                          On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
                          "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                          >int chartoval(char ch)
                          >{
                          > int answer = ch - '0';
                          > assert(answer >= 0 && answer <= 9);
                          > return answer;
                          >}
                          [snip]
                          I'd also check *before* converting the
                          character to an int, probably using isdigit().
                          isdigit() could return true for locale-specific digits, which
                          could be distinct from the '0' - '9' Western Arabic numerals in
                          the basic character set, for which the ch - '0' trick doesn't
                          work.
                          >
                          Really ? I don't think so:
                          >
                          7.4.1.5p2: The isdigit function tests for
                          any decimal-digit character (as defined in 5.2.1).
                          >
                          5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
                          >
                          isxxx functions that have locale specific behaviour are noted as such.
                          isdigit and iscntrl do not seem to have locale specific behaviour.
                          >
                          I cross post to comp.std.c to get
                          an authoritative answer on this question
                          relating to the C Standard.
                          But there's no ambiguity in the C standard
                          on this particular matter.

                          --
                          pete

                          Comment

                          • Charlie Gordon

                            #14
                            Re: Problem with atoi()

                            "pete" <pfiland@mindsp ring.coma écrit dans le message de news:
                            46E52328.6760@m indspring.com...
                            Charlie Gordon wrote:
                            >>
                            >"Army1987" <army1987@NOSPA M.ita écrit dans le message de news:
                            >pan.2007.09.10. 09.26.24.876899 @NOSPAM.it...
                            On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
                            >"Malcolm McLean" <regniztar@btin ternet.comwrite s:
                            >>int chartoval(char ch)
                            >>{
                            >> int answer = ch - '0';
                            >> assert(answer >= 0 && answer <= 9);
                            >> return answer;
                            >>}
                            [snip]
                            >I'd also check *before* converting the
                            >character to an int, probably using isdigit().
                            isdigit() could return true for locale-specific digits, which
                            could be distinct from the '0' - '9' Western Arabic numerals in
                            the basic character set, for which the ch - '0' trick doesn't
                            work.
                            >>
                            >Really ? I don't think so:
                            >>
                            >7.4.1.5p2: The isdigit function tests for
                            >any decimal-digit character (as defined in 5.2.1).
                            >>
                            >5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
                            >>
                            >isxxx functions that have locale specific behaviour are noted as such.
                            >isdigit and iscntrl do not seem to have locale specific behaviour.
                            >>
                            >I cross post to comp.std.c to get
                            >an authoritative answer on this question
                            >relating to the C Standard.
                            >
                            But there's no ambiguity in the C standard
                            on this particular matter.
                            I agree, but the wording is somewhat convoluted, Army1987 got it wrong, and
                            it took me too long to verify that isdigit is not infected with stupid
                            broken Locale stuff.

                            Why refer to the very long section 5.2.1 instead of explicitly describe the
                            digits '0' to '9' . Simplicity helps.

                            --
                            Chqrlie.


                            Comment

                            • André Gillibert

                              #15
                              Re: Problem with atoi()

                              Charlie Gordon wrote:
                              >>>int chartoval(char ch)
                              >>>{
                              >>> int answer = ch - '0';
                              >>> assert(answer >= 0 && answer <= 9);
                              >>> return answer;
                              >>>}
                              >[snip]
                              >>I'd also check *before* converting the character to an int, probably
                              >>using isdigit().
                              >isdigit() could return true for locale-specific digits, which
                              >could be distinct from the '0' - '9' Western Arabic numerals in
                              >the basic character set, for which the ch - '0' trick doesn't
                              >work.
                              >
                              Really ? I don't think so:
                              >
                              7.4.1.5p2: The isdigit function tests for any decimal-digit character (as
                              defined in 5.2.1).
                              >
                              5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
                              >
                              They're "contiguous " on ASCII or BCDIC based character sets, but on other
                              character sets, they may not be.
                              isxxx functions that have locale specific behaviour are noted as such.
                              isdigit and iscntrl do not seem to have locale specific behaviour.
                              >
                              That doesn't change the fact that ch - '0' is not guaranteed to portably
                              work.
                              I cross post to comp.std.c to get an authoritative answer on this
                              question
                              relating to the C Standard.
                              comp.lang.c is full of experts of the C standard for discussions about
                              coding in standard C.
                              comp.std.c is for discussion about the C standard, not about coding in
                              standard C.

                              [followup set to comp.lang.c]


                              --
                              You can contact me at <tabkanDELETETH ISnaz@yahoDELET ETHATo.fr>

                              Comment

                              Working...