So, my problem is whenever I want to print %s (char *), the program shows an empty string. Here is my code and I numbered all the questions inside the comments in the program
#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;
}
So, my question is how to manage these problems?
If you change your code a bit
for(int j = 0; j < size; j++){
printf("%c 0x%02x\n", binary[j], binary[j]); // but this works(2)
}
you will see the leading '\\0'
in your result:
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
So there is something wrong in your function decToBinary
.
You count with i
from the end of the array, so depending on the input you may not reach the beginning of the array.
You can try return binary+i+1;
at the end of decToBinary
. This will return a pointer to the character you have written last. Note that you should not iterate over size
characters of the result but only up to a '\\0
.
This is not the final solution because you use malloc
to allocate the character array in decToBinary
. This means you should return the pointer you got from malloc
and call free
in the calling function.
To allow this you can use an additional loop that copies all characters from index i+1
up to a '\\0'
to the start of the array.
for(i++, j=0; binary[i]; i++, j++) {
binary[j] = binary[i];
}
binary[j] = '\0';
/* ... */
return binary;
Additional note:
Code like this creates a memory leak because you don't free
the results of repeated calls to decToBinary
:
for(int i = 0; i < size; i++){
printf("With function: %c", decToBinary(n, size)[i]);
}
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.