Hi,
I'm facing some problem while returning string. I want to return final string value and print it. Below is the code that i tried,
int main()
{
int Input = 0;
//Tried using tmpstr also
//char *tmpStr = NULL;
scanf_s("%d", &Input);
//Tried as below also but it was crashing
//tmpStr = BinaryFormat((void*)Input);
//Tried as below also but it was crashing and showing only first character
//tmpStr = (char*)BinaryFormat((void*)Input);
//printf("\nThe Binary format for the given is : %s", tmpStr); //It was crashing here
//Trying to print the number in binary format
printf("\nThe Binary format for the given is : %s", BinaryFormat((void*)Input) ); //Its crashing here
}
//
//Coverting the given input to Binary data
//
char* BinaryFormat(void *Input)
{
char *binFormat = NULL;
int num = (int)Input;
//Counting number of bits needed
int bitCount = BitCount((int)Input); //This will return number of digits needed. This is separate fn
//Allocating Input based on number of digit(bit value) needed
//binFormat = (char*) malloc(bitCount*sizeof(char)+1);
binFormat = (char*) calloc(1,bitCount+1);
if (Input)
{
while(bitCount)
{
//Filling binFormat either with 1's or 0's based on number
binFormat[bitCount-1] = num&1 ? 1 : 0;
num = num >> 1;
bitCount--;
}
}
//returning final value as string
return binFormat;
}
Thanks in advance.