Problem with atoi()

This is a discussion on Problem with atoi() within the C Programming forums, part of the General Programming Boards category; My program compiles with one warning: passing arg 1 of `atoi' makes pointer from integer without a cast. When I ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Problem with atoi()

    My program compiles with one warning: passing arg 1 of `atoi' makes pointer from integer without a cast.
    When I run the program it crashes and the line causing the problems looks like this:
    Code:
    map[y][x] = atoi(rdBuffer[x]);
    map is an 2d array of integers and rdBuffer is a char array which gets a string from a file.

    If anyone could help me with the cast I would really appreciate it!

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    rdBuffer[x] is a char, atoi needs a char pointer. you should either send just rdBuffer or send (rdBuffer + x) if you want to start from the x offset of the char array.

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The cause of the error is that atoi() requires a char * argument, and rdBuffer[x] is a char. Assuming that x is where the string that you wish to convert to an int starts within rdBuffer, something like:
    Code:
    map[y][x] = atoi(&rdBuffer[x]);
    However, given that x is also used as an index in the access to map, I'm guessing what you really are looking for is the value of a digit character at position x within the input buffer, in which case you're really looking for something more like:
    Code:
    map[y][x] = rdBuffer[x] - '0';
    Note that this does no error/range checking, so non-ASCII digit characters will end up giving you bogus values in your map. You should check for these.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    It now compiles without warnings, thank you both for your fast replies!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with atoi()?
    By ruthgrin in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2006, 11:25 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21