Creating an atoi function

后端 未结 5 1646
感情败类
感情败类 2021-01-19 04:03

I\'m attempting to create my own atoi function. With the following I\'m getting a return value of 0. Whatever I change the number variable within the function is what I get

相关标签:
5条回答
  • 2021-01-19 04:23

    As I thought, the problem is in your call.

    Change your main to.

    int main()
    {
        char number[MAXSIZE];
        int num;
    
        printf("Please enter a number:\n");
        scanf("%s", number);
        num = atoi_me(number);
        printf("%d", num);
        return 0;
    }
    

    Other than this it's not a good idea to use scanf - http://c-faq.com/stdio/scanfprobs.html . In this case you should use fgets.

    0 讨论(0)
  • 2021-01-19 04:25

    This was not an issue with your atoi_me() function, but an issue with how you obtained your input. Your implementation shows some weakness in your understanding of how scanf() works. That's not a problem in of itself, making mistakes is part of the learning process after all.

    It is generally safer to gather your input into a buffer first, because scanf() from the standard input relies too much on the user of the program to type input exactly the way you expect it. In this case, there is not much harm since you only want a single line of input. But, usually, a program will process multiple lines of input, and scanf() can jam when an error occurs. So, you can use something like this to get your line of input instead:

    char line[MAXLINESIZE];
    
    if (fgets(line, MAXLINESIZE, stdin) == 0) {
        fprintf(stderr, "no input was provided!\n");
        return 0;
    }
    

    As mentioned elsewhere, %c is the wrong format specifier to use for the input you are gathering. Since you want decimal digits, the *scanf() family has a format specifier to allow you to only collect those characters.

    char number[MAXSIZE];
    
    if (sscanf(line, " %[0-9]", number) != 1) {
        fprintf(stderr, "no number found in input: %s", line);
        return 0;
    }
    

    Here, I use the line that was retrieved with fgets(), and parse out the part of the input that contains the digits. The leading space causes sscanf() to skip over space characters leading up to the digits.

    0 讨论(0)
  • 2021-01-19 04:28

    Some comments:

    int atoi_me(const char *numstring)...
    

    Better to use const type pointer, as you don't intend to modify string contents.

    int main()
    {
        char number[MAXSIZE]; // array of chars
        int num;
    
        printf("Please enter a number:\n");
        scanf("%s", number);     // enter a string, not a char
        num = atoi_me(number);   // pointer to char, not pointer to pointer
        printf("%d", num);
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-19 04:35
    1. You're declaring an array of char *, that is, an array of strings, rather than a single string. You probably want:

      char number[MAXSIZE];
      
    2. Your scanf format string is wrong. If you want to read a string, you should use %s. %c reads only a single character.

    3. Your scanf parameter is wrong - pass number itself (or &number[0] if you prefer), not &number.

    4. The parameter you're passing to atoi_me is wrong. Call it with number (or equivalently &number[0]), not *number.

    Putting all of that together, you should have a main routine something like this:

    int main(void)
    {
        char number[MAXSIZE];
        int num;
        printf("Please enter a number: ");
        scanf("%s", number);
        num = atoi_me(number);
        printf("%d\n", num);
        return 0;
    } 
    

    Editorial notes: You have a potential buffer overflow with the scanf line. You'd be better off using a function like fgets(3) that makes it easy to protect against that kind of problem.

    atoi(3) also traditionally supports negative numbers (with a leading -) and an optional leading + for positive numbers, which your implementation doesn't handle.

    0 讨论(0)
  • 2021-01-19 04:48

    Since number is supposed to be a char array,

    • you should declare it as char number[MAXSIZE];,
    • you shouldn't call your function with &number but with number, directly : atoi_me(number);
    0 讨论(0)
提交回复
热议问题