0

I'm trying to implement atoi function in C. However, whenever i use a test string "856" the resulting int will be 855. I tried many test cases but the outcome will be the same (e.g "4756"->4755). Below code is my incorrect solution.

#include <math.h>
#include <stdio.h>
int count = 0;
void atoi(char * str){
    static int power = 0;
    char * next = str;
    next++;
    if(*next != '\0' && next != NULL){
        atoi(next);
        power++;
        //printf("%f\n",((*str) - '0') * pow(10, power)); 
        count += ((*str) - '0') * pow(10, power);
    }
    else{
        //printf("%f\n",((*str) - '0') * pow(10, power));
        count += ((*str) - '0') * pow(10, power);
    }
}
int main(){
    char * string = (char *)malloc(sizeof(char)*3);
    string = "457";
    atoi(string);
    printf("%d",count);
}

Could you please check why did this happen?

Rebec
  • 1
  • 2
    You're using the **pow** function. Don't. Instead multiply the multiplier by 10... – Antti Haapala -- Слава Україні Jun 28 '17 at 21:17
  • I.e. `pow(10, 2)` rounds to 99 on your platform (a particular bad `pow` implementation) – Antti Haapala -- Слава Україні Jun 28 '17 at 21:20
  • 1
    Also, you're not allowed to define your own function by name *`atoi`* as it clashes with the standard library function - you *must* name it something else. Also, return the value, with `return count;`, do not store it into a global variable. – Antti Haapala -- Слава Україні Jun 28 '17 at 21:23
  • Also, your algorithm doesn't work properly recursively, as `power` is static state... - your second `atoi` invocation is going to fail – Antti Haapala -- Слава Україні Jun 28 '17 at 21:26
  • Side note: Consider `if(*next != '\0' && next != NULL){` and the _second_ test of `next != NULL`. If `next was `NULL`, the prior `*next` likely would have caused trouble like killed the program. IOWs, the test was too late. – chux - Reinstate Monica Jun 28 '17 at 21:54
  • @AnttiHaapala - it's common practice to shadow standard library functions with optimized implementations... `malloc` is the perfect example (`atoi` is a poor example). It might not be a smart idea, but it's allowed (or should be allowed) and mostly supported by using `weak` symbols in the standard library. – Myst Jun 28 '17 at 22:26
  • @Myst this is specifically listed as undefined behaviour in C standard. Naturally if you replaced it with a completely compatible implementation, then it could just work. Here - not even, because the `atoi` is clearly not even compatible with the existing one. – Antti Haapala -- Слава Україні Jun 28 '17 at 22:38
  • @AnttiHaapala I wasn't aware that this is "Undefined Behavior". My memory must be playing tricks on me lately. I'll look it up. And yes, OP's `atoi` doesn't even return an `int` - it's all `a` no `i`, so it will probably break any standard function (such as `printf` that uses `atoi` internally... :-) – Myst Jun 28 '17 at 22:56
  • 1
    @Myst "All identifiers with external linkage in any of the following subclauses are reserved for use as identifiers with external linkage if any of them are used by the program. None of them are reserved if none of them are used." and J.2. says "The program declares or defines a reserved identifier, other than as allowed by [7.1.3 or 7.1.4]" – Antti Haapala -- Слава Україні Jun 28 '17 at 23:15
  • @AnttiHaapala thanks! – Myst Jun 28 '17 at 23:35

0 Answers0