Problem with atoi()

Discussion in 'C Programming' started by tolkien, Sep 9, 2007.

  1. tolkien

    tolkien Guest

    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"
     
    tolkien, Sep 9, 2007
    #1
    1. Advertising

  2. tolkien

    Army1987 Guest

    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
     
    Army1987, Sep 9, 2007
    #2
    1. Advertising

  3. tolkien

    Joe Wright Guest

    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 ---
     
    Joe Wright, Sep 9, 2007
    #3
  4. tolkien

    tolkien Guest

    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)
     
    tolkien, Sep 9, 2007
    #4
  5. tolkien

    runner Guest

    "tolkien" <> wrote in message
    news:...
    > 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[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.
     
    runner, Sep 9, 2007
    #5
  6. tolkien

    tolkien Guest

    thank you very much!!!!!
     
    tolkien, Sep 9, 2007
    #6
  7. "tolkien" <> wrote in message
    news:...
    >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.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Sep 9, 2007
    #7
  8. "Malcolm McLean" <> writes:
    > "tolkien" <> wrote in message
    > news:...
    >>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_Keith) <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"
     
    Keith Thompson, Sep 9, 2007
    #8
  9. tolkien

    Army1987 Guest

    On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
    > "Malcolm McLean" <> writes:
    >> 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
     
    Army1987, Sep 10, 2007
    #9
  10. Army1987 <> writes:
    > On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
    >> "Malcolm McLean" <> writes:
    >>> 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_Keith) <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"
     
    Keith Thompson, Sep 10, 2007
    #10
  11. "Army1987" <> a écrit dans le message de news:
    ...
    > On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
    >> "Malcolm McLean" <> writes:
    >>> 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.
     
    Charlie Gordon, Sep 10, 2007
    #11
  12. tolkien

    pete Guest

    Malcolm McLean wrote:
    >
    > "tolkien" <> wrote in message
    > news:...
    > >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
     
    pete, Sep 10, 2007
    #12
  13. tolkien

    pete Guest

    Charlie Gordon wrote:
    >
    > "Army1987" <> a écrit dans le message de news:
    > ...
    > > On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
    > >> "Malcolm McLean" <> writes:
    > >>> 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
     
    pete, Sep 10, 2007
    #13
  14. "pete" <> a écrit dans le message de news:
    ...
    > Charlie Gordon wrote:
    >>
    >> "Army1987" <> a écrit dans le message de news:
    >> ...
    >> > On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
    >> >> "Malcolm McLean" <> writes:
    >> >>> 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.
     
    Charlie Gordon, Sep 10, 2007
    #14
  15. 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 <>
     
    André Gillibert, Sep 10, 2007
    #15
  16. André Gillibert wrote:

    > They're "contiguous" on ASCII or BCDIC based character sets, but on
    > other character sets, they may not be.
    >


    Excuse me. Actually, the C standard forbids such character sets.
    They have to be contiguous.

    --
    You can contact me at <>
     
    André Gillibert, Sep 10, 2007
    #16
  17. André Gillibert said:

    <snip>

    > ch - '0' is not guaranteed to portably work.


    I think I'm right in saying you no longer maintain this position but,
    just to be clear, it *is* guaranteed to work if the intent is to
    convert 'x' to x where x is in the range 0-9.

    <snip>

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

    Richard Bos Guest

    "Charlie Gordon" <> wrote:

    > "pete" <> a écrit dans le message de news:
    > ...
    > > Charlie Gordon wrote:
    > >>
    > >> "Army1987" <> a écrit dans le message de news:
    > >> ...
    > >> > 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.


    Possibly because if you want to change the definition of "digit" (for
    example, to allow locale-specific digits, as above), you only need to
    change it in one place.

    Richard
     
    Richard Bos, Sep 10, 2007
    #18
  19. Richard Heathfield wrote:

    > André Gillibert said:
    >
    > <snip>
    >
    >> ch - '0' is not guaranteed to portably work.

    >
    > I think I'm right in saying you no longer maintain this position


    Yes.
    I'm sorry to have posted this wrong material.

    > but,
    > just to be clear, it *is* guaranteed to work if the intent is to
    > convert 'x' to x where x is in the range 0-9.


    Yes.



    --
    You can contact me at <>
     
    André Gillibert, Sep 10, 2007
    #19
  20. "André Gillibert" wrote:
    > > 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.
    > ...
    > That doesn't change the fact that ch - '0' is not guaranteed to portably
    > work.


    Actually the C standard does require the digit codes to be contiguous
    and ascending, so it is guaranteed to work on any conforming C
    implementation.

    I argued against this requirement, which infringes on the codeset
    designer's turf, but lost the argument. (I would rather specify
    macros/functions to convert between digit characters and
    corresponding numeric interpretations.)
     
    Douglas A. Gwyn, Sep 10, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    8
    Views:
    43,582
  2. Magig Boatman
    Replies:
    1
    Views:
    482
    Russell Hanneken
    Jul 11, 2003
  3. Henry Jordon

    atoi problem

    Henry Jordon, Jul 13, 2004, in forum: C++
    Replies:
    6
    Views:
    703
    David Rubin
    Jul 13, 2004
  4. Sanchit

    Problem with atoi and strlod

    Sanchit, Apr 13, 2008, in forum: C Programming
    Replies:
    6
    Views:
    426
    Keith Thompson
    Apr 14, 2008
  5. Marcelo De Brito

    A Minor Problem With atoi() And Negative Numbers

    Marcelo De Brito, Mar 7, 2010, in forum: C Programming
    Replies:
    12
    Views:
    4,851
    Nick Keighley
    Mar 9, 2010
Loading...

Share This Page