atoi() problem

This is a discussion on atoi() problem within the C Programming forums, part of the General Programming Boards category; I am having problems using the atoi() function where i have 2 char arrays which i want to turn into ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    atoi() problem

    I am having problems using the atoi() function where i have 2 char arrays which i want to turn into integers. So say
    Code:
    char window[4] = ('1','2','3','4')
    char size[4]= ('0','0','9','0')
    
    newWindow = atoi(window);
    newSize = atoi(size);
    Doing a print on newWindow gives me a value of = "1234009" and newSize = '90'. Do I have to close atoi? It seems like its appending everything to the same value. Which is not what we want.

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TaiL View Post
    It seems like its appending everything to the same value.
    Yep, because window is not a C string and atoi() operates on C strings. A C string is a sequence of characters with '\0' (the null terminator) at the end.

    The reason for this is that the program does not "know" how long the string is supposed to be (even if you give it a declared length); it just keeps reading until it hits \0. This may seem silly but it makes sense from a performance stand-point; it would be less efficient if the processor had to look-up the length of a string and do a comparison every byte to see if it had reached the end.

    What is happening with the above code is the program puts two variables into memory, window and size. Window is 4 bytes (1234), and obviously the next thing in memory is size (0090).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    thanks. MK wasted an hour trying to figure it out, who woulda known all i needed was a NULL at the end of my . Thanks again.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TaiL View Post
    thanks. MK wasted an hour trying to figure it out, who woulda known all i needed was a NULL at the end of my . Thanks again.
    Don't think of stuff like that as a waste, think of it as a mnemonic aid
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Problem with atoi()?
    By ruthgrin in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2006, 12:25 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

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