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 :/
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 :/
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.
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 :/
You don't understand the minus symbol?Originally Posted by +Azazel+
Say you input "123" as the string.
- result = result * 10 + ('1' - '0')
- result = 0 * 10 + 1
- result = 1
- result = result * 10 + ('2' - '0')
- result = 1 * 10 + 2
- result = 12
- result = 12 * 10 + ('3' - '0')
- result = 12 * 10 + 3
- 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
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?
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
Yes?Code:1000 + 200 + 30 + 4 ----------- =1234
If you put that number into the function posted:
There will be 4 steps, as there are 4 digits:Code:result = 0; for(i = 0; i < n; i++) result = result * 10 + (arr[i] - '0');
If you understand all this, but don't understand (arr[i] - '0'), read on: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
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.
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.
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
It gives me 1. Why and how would I find the sizeof array through a pointer?Code:int my_getnbr(char *str) { int z = sizeof(*str); printf("%d", z); }
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.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).Code:char array[] = "12345";
--
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.