Thread: atoi returns incorrect result

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    Question atoi returns incorrect result

    language: C

    *s = "3 ..."
    expected zoom == 3
    result is 13. Why?

    image:
    http://oi58.tinypic.com/j8ljfa.jpg

    Code:
    zoom = (char) atoi(*s);
    same result 13

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Works for me:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main(void)
    {
        int zoom;
        const char *x;
        const char **s = &x;
        *s = "3 ...";
        zoom = atoi(*s);
        printf("%d\n", zoom);
        return 0;
    }
    Running the above program gives me the output of: 3
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    You didn't check the image which shows the code and what i see in watch panel while debuging.

    You didn't check what type is zoom.

    I did some change to the code as I tried to correct the problem:

    Code:
    char * zoom = &(w->task.temp.blocksInfo[w->task.temp.square_no].z);
    *zoom = (char) atoi(*s);
    The problem is that I expect * zoom to point to struct with z member. z meber is char.

    I expect the zoom should point to the z member and be set 3.

    But what I got is zoom: 0x3e28d8 "0x3e28d8 "\003đ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ.. ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\ rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\253\253\253\253\ 253\253\253\253""

    What am I doing wrong?

    In the image which I

    Edit:
    Actually I did not correctly said what I need. I need to set the w->task.temp.blocksInfo[w->task.temp.square_no].z via zoom pointer. Now I see it really works
    after the correction which I did (added pointer to zoom). But what does not look correct is the value of zoom. There is array instead single char.

    w->task.temp.blocksInfo[w->task.temp.square_no].z : 3 '\003'
    zoom : 0x3e28d8 "\003đ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ.. ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\ rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\253\253\253\253\ 253\253\253\253"
    Last edited by barracuda; 04-27-2015 at 08:31 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by barracuda
    You didn't check the image which shows the code and what i see in watch panel while debuging.

    You didn't check what's zoom.
    You did not post the smallest and simplest compilable program that demonstrates the problem.

    The reason why I wrote what I did was to show you how easy it is to see with a small and simple compilable example what you're doing right or wrong. When you face such problems, this is one of the tools that you can use to help you debug: set aside your current program and try to come up with the smallest and simplest program that you expect should compile or run with the expected result but which fails to compile or runs with an unexpected result.

    Anyway, checking the image: your arrow is wrong. On the first line of the variables being watched is zoom, which clearly has a value of 3. Your arrow that points to the assignment to zoom in your code actually should point to the previous line where you assign an address to zoom. This assignment does not make sense since zoom is a char, not a pointer.

    In your updated code snippet, you changed zoom to be a pointer to char. This could work, except that this means that you are actually storing an int in a char, but an int does not necessarily fit into a char.

    Quote Originally Posted by barracuda
    The problem is that I expect * zoom to point to struct with z member. z meber is char.

    I expect the zoom should point to the z member and be set 3.

    But what I got is zoom: 0x3e28d8 "0x3e28d8 "\003đ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\ rđ.. ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ ..ş\ rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş \rđ ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\253\253\253\25 3\ 253\253\253\253""

    What am I doing wrong?
    You probably examined or printed zoom instead of *zoom (or equivalently, the z member).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    since zoom is char* the VS tries to show it as a string.
    You are not interested in zoom, you are interested in *zoom

    Since *zoom is a char - Watch window will show you the contents of the char
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Ok, now it's clear. So CODE::BLOCKS IDE interprets the zoom as array. I see now. Thanks

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    OK, so this should be correct:
    Code:
    char * zoom = &(w->task.temp.blocksInfo[w->task.temp.square_no].z);
    *zoom = (char) atoi(*s);
    Typecast to fit integer to char. The allowed values are smaller then 128 so this is OK.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by barracuda
    Typecast to fit integer to char.
    This only suppresses the compiler warning, if any. If char is signed and your assumption does not hold, the behaviour is implementation defined.

    Quote Originally Posted by barracuda
    The allowed values are smaller then 128 so this is OK.
    You might want to store the return value of atoi in a temporary int variable anyway, then write an assertion to document your assumption, and only then assign that int variable to *zoom.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-04-2015, 05:20 PM
  2. Incorrect result in Bitwise operations
    By hjazz in forum C Programming
    Replies: 1
    Last Post: 10-30-2014, 12:27 AM
  3. incorrect result in program
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 02-15-2014, 08:46 PM
  4. Replies: 5
    Last Post: 03-06-2012, 04:44 PM
  5. Incorrect result with my program
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 03:46 PM

Tags for this Thread