简体   繁体   English

以文本格式打印数字的数字

[英]printing digits of a number in textual format

so I've been trying to write this piece of code in c where the basic idea is that the user enters a positive integer and the program prints the digits in that number, and I've done it recursively but the thing is, when I compile it, I get no errors but the output is just some trash, on the other hand, I've tried debugging and so I've set some breakpoints and I keep getting fine results all the way and correct output while debugging, unlike when trying to compile the exact same piece of code If anyone has any idea why this is so, it would be really appreciated 所以我一直试图用c编写这段代码,其基本思想是用户输入一个正整数,程序会打印该数字中的数字,而我已经递归地完成了,但是事情是,当我编译它,没有任何错误,但是输出只是一些垃圾,另一方面,我尝试了调试,因此我设置了一些断点,并且在调试过程中始终获得良好的结果并纠正了输出,这与尝试编译完全相同的代码如果有人知道为什么会这样,将不胜感激

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *numToWord (int number){
    char *newDigit = NULL;
    switch(number){
    case 0:
        newDigit = malloc (5*sizeof(char));
        sprintf(newDigit,"zero");
        break;
    case 1:
        newDigit = malloc (4*sizeof(char));
        sprintf(newDigit,"one");
        break;
    case 2:
        newDigit = malloc (4*sizeof(char));
        sprintf(newDigit,"two");
        break;
    case 3:
        newDigit = malloc (6*sizeof(char));
        sprintf(newDigit,"three");
        break;
    case 4:
        newDigit = malloc (5*sizeof(char));
        sprintf(newDigit,"four");
        break;
    case 5:
        newDigit = malloc (5*sizeof(char));
        sprintf(newDigit,"five");
        break;
    case 6:
        newDigit = malloc (4*sizeof(char));
        sprintf(newDigit,"six");
        break;
    case 7:
        newDigit = malloc (6*sizeof(char));
        sprintf(newDigit,"seven");
        break;
    case 8:
        newDigit = malloc (6*sizeof(char));
        sprintf(newDigit,"eight");
        break;
    case 9:
        newDigit = malloc (5*sizeof(char));
        sprintf(newDigit,"nine");
        break;
    }
    return newDigit;
}

char * writeAbsolute (int number) {
    char * word = NULL; // variable where the return value will be stored, must clean in main part
    int digit;
    digit = number % 10;
    if (number){
        number /= 10;
        word = writeAbsolute (number);
        char *newDigit = NULL;
        char *newWord = NULL;
        newDigit = numToWord(digit);
        if (word){
            int numOfLetters = (int)( strlen(newDigit)+strlen(word) );
            newWord = (char*) malloc(numOfLetters * sizeof(char));
            sprintf(newWord,"%s %s",word,newDigit);
            word = newWord;
        }
        else{
            word = newDigit;
        }
        free (newWord);
        free (newDigit);
    }
    return word;
}


int main() {
    int number;
    char * word;
    printf("Enter an integer:\n");
    scanf("%d",&number);
    word = writeAbsolute(number);
    printf("%s",word);
    return 0;
}

The first issue is that you are not allocating enough memory for all your strings. 第一个问题是您没有为所有字符串分配足够的内存。 The string "1234" should be allocated with 5 characters due to the inherent trailing '\\0' terminating byte. 由于固有的尾随'\\0'终止字节,字符串"1234"应该分配5个字符。 Simply add at least one to all your string allocation lengths: 只需在所有字符串分配长度中至少添加一个即可:

newDigit = malloc(5*sizeof(char));
sprintf(newDigit,"six ");
...
int numOfLetters = (int) (strlen(newDigit) + strlen(word) + 1);

Also note that sizeof(char) is defined to be 1 . 还要注意, sizeof(char)定义为1

The second issue is that your are freeing your strings immediately after using them in this code: 第二个问题是您在此代码中使用字符串后立即释放它们:

if (word) {
    ...
    newWord = (char *) malloc(...);
    ...
    word = newWord;
}
else {
    word = newDigit;
}

free (newWord);
free (newDigit);

Assuming word is not NULL , when you free newWord at the end then word becomes invalid as it just points to the same string. 假设word不为NULL ,则当您在最后释放newWord时,由于它仅指向同一字符串,因此word无效。 Thus, the next time you try to use word you invoke undefined behaviour by trying to use previously freed memory. 因此,下次您尝试使用word时,将通过尝试使用先前释放的内存来调用未定义的行为。

What you probably want in this case is something like: 在这种情况下,您可能想要的是:

if (word) {
    int numOfLetters = (int)( strlen(newDigit)+strlen(word) + 1);
    newWord = (char*) malloc(numOfLetters);
    sprintf(newWord,"%s%s", word, newDigit);
    word = newWord;
    free(newDigit);
}
else {
    word = newDigit;
}
// Don't free anything else here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM