简体   繁体   中英

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

#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. 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 .

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. Thus, the next time you try to use word you invoke undefined behaviour by trying to use previously freed memory.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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