Thread: Merging into a number.

  1. #16
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    bah, I think I am destined to never get this. I spent some time dissecting that loop and trying to understand it but I dont get it :/

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure someone here can explain exactly every part of it, but please do explain what you think it does first - because otherwise we'll be spending a lot of time explaining what you already know, and perhaps not enough to explain what you need to know.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    I dont understand the "dash - " and I dont really understand how it solves my problem.

    Looking through what the atoi() function does, I think I need exactly that (not sure though). Where can I see the source code for it?

    In the end this is just a curious question, and it its too hard for me I might as well dump it :/

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by +Azazel+
    I dont understand the "dash - " and I dont really understand how it solves my problem.
    You don't understand the minus symbol?


    Say you input "123" as the string.
    1. result = result * 10 + ('1' - '0')
      1. result = 0 * 10 + 1
      2. result = 1
    2. result = result * 10 + ('2' - '0')
      1. result = 1 * 10 + 2
      2. result = 12
    3. result = 12 * 10 + ('3' - '0')
      1. result = 12 * 10 + 3
      2. result = 123

    In the end, result = 123 which is the converted integer value of the string "123".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Ohhh! I see, yes. Thank you! But what if I dont know the size of the inputted number? It could be 2,3,4,5 digits long (Its an int so 5 would be max). How would I determine that?

  6. #21
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by +Azazel+ View Post
    bah, I think I am destined to never get this. I spent some time dissecting that loop and trying to understand it but I dont get it :/
    You have to realize that every base-10 number(the numbering system that we use) is composed of ones, tens, hundres, thousands, etc.

    Eg: 1234 = (1 * 1000) + (2 * 100) + (3 * 10) + (4 * 1).

    Just try that in the calculator, or by hand

    Code:
       
       1000
      + 200
      +  30
      +   4
    -----------
      =1234
    Yes?

    If you put that number into the function posted:

    Code:
    result = 0;
    for(i = 0; i < n; i++)
            result = result * 10 + (arr[i] - '0');
    There will be 4 steps, as there are 4 digits:

    Code:
    result = 0   * 10 + 1 = 0    + 1 = 1
    result = 1   * 10 + 2 = 10   + 2 = 12
    result = 12  * 10 + 3 = 120  + 3 = 123
    result = 123 * 10 + 4 = 1230 + 4 = 1234
    result = 1234
    If you understand all this, but don't understand (arr[i] - '0'), read on:

    In essence, all things in a computer are numbers. Your compiler and C programs 'convert' chars to integers, as integers are native to the computer(Which is why, when possible, always use ints instead of char). With this in mind, you can look at an ascii chart and find the integer value for for 0(zero), which is 48.
    Now, if you look at the ASCII chart at take a random number on it, 1 for example, you will see that its integer value is 49, right after 0. If you subtract 48 from 49, you get 1, which is why the algorithm he posted works.

    FYI, ASCII stands for American Standard Code (for) Information Interchange. Basically, this standard simply controls which integers correspond to which letters. The ASCII table lists the characters and what their integer, hex and octal equivalents are.

    There are numerous other standards though, such as EBCDIC for example, and there are also other ASCII sets(Often called Extended ASCII sets) which define the sets for various foreign alphabets.

    Any further questions? - Google, then ask.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The glibc source code is available freely through the CVS web interface, and here's the atoi code (you have to click "download" on this page):
    http://sourceware.org/cgi-bin/cvsweb...?cvsroot=glibc

    It's not going to help you much, as it's calling strtol(), which calls a more generic internal version:
    http://sourceware.org/cgi-bin/cvsweb...&cvsroot=glibc

    Now, if you think that little loop was hard to understand, you won't have much chance to understand the above code... It's quite a lot more complex.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Alright. Well Im almost done but one little problem.

    char array[5] = {'3','5','0','7','0'};

    If I do sizeof(array) it gives me "5". (Correct)

    But if I do


    Code:
    int my_getnbr(char *str)
    {
        int z = sizeof(*str);
        printf("&#37;d", z);
    }
    It gives me 1. Why and how would I find the sizeof array through a pointer?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's correct. Sizeof only tells you the size of whatever you asked for. In this case, what's the size of (*str) - which is one. The size of the array, on the other hand, is 5, because the array has 5 elements of one byte each.

    You need to pass the size in, or use some other method to know the size (e.g.
    Code:
    char array[] = "12345";
    which would make the a string that is zero-terminated, and you can go back to checking for a zero at the end of the string).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM