繁体   English   中英

带字符串的 Printf 不起作用(2 种方式:函数返回值和一个值)

[英]Printf with string does not work(2 ways: function return value and just a value)

所以,我的问题是每当我想打印 %s (char *) 时,程序都会显示一个空字符串。 这是我的代码,我对程序注释中的所有问题进行了编号

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *decToBinary(char *num, int size);

int main(){
    char *n;
    n = (char *)malloc(5);  
    //memset(n, 0, 5);
    printf("Enter n: ");

    // getting the number as a string
    scanf("%s", n);
    int size = 33;
    char *binNum;
    binNum = (char *)malloc(size);  
    memset(binNum, 0, size);

    //copying the (char *) which is returned by function to char binNum[33] 
    strcpy(binNum, decToBinary(n, size));

    //sprintf(binNum, "%s", decToBinary(n, size));

    // Printing the FUNCTION variable as a character in a loop. WORKS(WHY?)(3)
    for(int i = 0; i < size; i++){
        printf("With function: %c", decToBinary(n, size)[i]);
    }
    //Printing the variable binNum as a string <--------- DOES NOT WORK. JUST EMPTY MESSAGE(WHY?)(4)
    printf("In main: %s\n", binNum);

    // Printing the variable binNum as a character in a loop <------------ DOES NOT WORK(5)
    for(int i = 0; i < size; i++){
        printf("with variable: %c", binNum[i]);
    }
    printf("\n");
}

char *decToBinary(char *num, int size){
    // convert decimal to binary
    int i = size - 2;
    int remainder;
    int decimal;
    char *binary;
    int n;
    n = atoi(num); 
    binary = (char *)malloc(size);
    binary[size - 1] = '\0';
    memset(binary, 0, size);
    do{
        remainder = n % 2;
        n /= 2;
        binary[i] = remainder + '0';
        i--;
    }while(n);
    printf("decToBinary = %s\n", binary); // does not work as string (1)
    for(int j = 0; j < size; j++){
        printf("%c", binary[j]); // but this works(2)
    }
    printf("\n");
    return binary;   
}
  • (1) 函数中不显示字符串。 即使我使用 "\\n" 刷新缓冲区
  • (2) 但是当我使用“for”循环并使用 %c 显示数组的每个元素时 - 它可以工作。 但这不是让它工作的好方法
  • (3) 在 main() 函数中,函数返回值的每个元素的 printf 工作正常,即使我不知道它是如何工作的,因为据我所知,该函数被调用了 33 次,返回值是 33次,在这里我只是一个一个地阅读。 我认为这是不好的做法。
  • (4) 所以我将函数的返回值复制到变量 binNum 并尝试使用 %s 显示它,但再次显示空字符串。
  • (5) 与变量相同,但使用“for”循环和 %c。 也没有工作

所以,我的问题是如何管理这些问题?

如果你稍微改变你的代码

    for(int j = 0; j < size; j++){
        printf("%c 0x%02x\n", binary[j], binary[j]); // but this works(2)
    }

您将在结果中看到领先的'\\0'

With function: 1decToBinary =
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
1 0x31
0 0x30
0 0x30
0 0x30
0 0x30
0 0x30
0 0x30
0 0x30
0 0x30
1 0x31
1 0x31
 0x00

所以你的函数decToBinary

您从数组的末尾用i计数,因此根据输入,您可能无法到达数组的开头。

你可以试试return binary+i+1; decToBinary 这将返回一个指向您最后写入的字符的指针。 请注意,您不应迭代结果的size字符,而应迭代到'\\0

这不是最终的解决方案,因为您使用mallocdecToBinary分配字符数组。 这意味着您应该返回从malloc获得的指针并在调用函数中调用free

为此,您可以使用一个额外的循环,将索引i+1'\\0'所有字符复制到数组的开头。

    for(i++, j=0; binary[i]; i++, j++) {
        binary[j] = binary[i];
    }
    binary[j] = '\0';

    /* ... */

    return binary;

补充说明:

像这样的代码会造成内存泄漏,因为您不会free重复调用decToBinary的结果:

    for(int i = 0; i < size; i++){
        printf("With function: %c", decToBinary(n, size)[i]);
    }

暂无
暂无

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

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