0
#include<stdio.h>
#include<stdlib.h>
unsigned int *bin(int);
int main(void)
{
  unsigned int n=0,*i=NULL;
  printf("Enter no:");
  scanf("%d",&n);
  i=bin(n);
  printf("Binary no: %d\n",*i);
  return 0;
}
unsigned int *bin(int n)
{
  unsigned int i=0,j=0;
  static unsigned int *result=NULL;
  result=(unsigned int*)malloc(1*sizeof(unsigned int));
  printf("Result=%p\n",result);
  j=(unsigned int)result;
  for(i=(1<<31);i>0;i=(i>>1))
  {
    if(n & i)
    {
      *result=1;
      result++;
    }
    else
    {
      *result=0;
      result++;
    }
}
  result=(unsigned int*)j;
  printf("Result=%p\n",result);
  return result;
}
Output : 
Enter no:6
Address of Result=0x2576010
Address of Result=0x2576010
Binary no: 0

The purpose of this program is to convert decimal number to binary number.The main function is calling the bin() function to convert decimal to binary.

Logic of Code :- Let us take unsigned integer (32 bit), which consist of 0-31 bits. To print binary representation of unsigned integer, start from 31th bit, check whether 31th bit is ON or OFF, if it is ON print “1” else print “0”. Now check whether 30th bit is ON or OFF, if it is ON print “1” else print “0”, do this for all bits from 31 to 0, finally we will get binary representation of number.

I am confused how much space should be malloced to store 32bits of integer.And how to free memory allocated to result.Please help me out with this code.

Shubham S. Naik
  • 341
  • 1
  • 4
  • 14
  • 1
    `j=(unsigned int)result;`... why?? pointer to integer conversion is highly implementation dependent. use `uintptr_t` if needed. – Sourav Ghosh Nov 10 '16 at 07:06
  • Don't use casts to silence compiler errors. Instead, ask what you are doing wrong. – n. m. could be an AI Nov 10 '16 at 07:17
  • @SouravGhosh better ask why `j` needs to be an integer of any type in the first place. – n. m. could be an AI Nov 10 '16 at 07:18
  • `result=(unsigned int*)malloc(1*sizeof(unsigned int));` you are allocating 4 bytes (or maybe 8 bytes depending on your platform) of memory here, that's not enough. But there are mor eproblems, and the code is really weird. – Jabberwocky Nov 10 '16 at 07:18
  • How many integers have you allocated? *Print out that number*. Is that what you wanted to do? Then again, how many integers have you printed in the final printf? Is it the same number you have allocated? If not, why not? – n. m. could be an AI Nov 10 '16 at 07:21
  • @Michael as far as I know `malloc(sizeof(int))` (to put it simply) can be 2 or 4. Eight, I am really not sure. – Djee Nov 10 '16 at 07:28
  • @Djee it can also be 8 on a 64 bit platform, see [this SO article](http://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – Jabberwocky Nov 10 '16 at 07:40
  • What is your opinion about the static pointer *result which I have declared in function bin(). It works even if I dont declare it static. But if its not declared static its class becomes automatic,and its scope will be inside the function bin() and as soon as function bin() finishes execution it will be destroyed correct? But still it works even if its automatic pointer. Why so? – Shubham S. Naik Nov 10 '16 at 10:08
  • @Michael. Meaning `unsigned int` can reach 2^64-1? Sorry I ignored this. I thought you had to write `unsigned long long` to reach this range. Thank you. Hence my advice in C/C++: only deal with `sizeof`of integer values on `short`, `long`, `long long` types. Never on `int` types. – Djee Nov 10 '16 at 10:51
  • @ShubhamS.Naik `result` doesn't need to be static. It just contains a _value_ that is returned by the `bin` function. That value continues of course to exist once the function goes out of scope. – Jabberwocky Nov 10 '16 at 11:00

3 Answers3

2

You will need to allocate memory of 32bytes(at least) to store 1 or 0 corresponding to each bit in n considering the datatype of 1 and 0 be char. Moreover I will recommend to have int as the data type and allocate 32x4(bytes) of memory. Here is the probably your final code should look like:

#include<stdio.h>
#include<stdlib.h>
unsigned int *bin(int);
int main(void)
{
  unsigned int n=0,*result =NULL;
  printf("Enter no:");
  scanf("%d",&n);
  result =bin(n);
  printf ("binary representation is: ");
  int i;
  for ( i=0;i<32;i++)
    printf("%d ",result[i]);
  free(result);
  return 0;
}

unsigned int *bin(int n)
{
  unsigned int i=0;
  static unsigned int *result=NULL;
  result=(unsigned int*)malloc(32*sizeof(unsigned int));
  printf("Result=%p\n",result);
  unsigned int* j=NULL;
  j=result;
  for(i=(1<<31);i>0;i=(i>>1))
  {
    if(n & i)
    {
      *j=1;
      j++;
    }
    else
    {
      *j=0;
      j++;
    }
}

  return result;
}
hrishi
  • 443
  • 2
  • 12
  • 1
    You should `free(result)` at the end of `main`. – Jabberwocky Nov 10 '16 at 07:43
  • What is your opinion about the static pointer *result which I have declared in function bin(). It works even if I dont declare it static. But if its not declared static its class becomes automatic,and its scope will be inside the function bin() and as soon as function bin() finishes execution it will be destroyed correct? But still it works even if its automatic pointer. Why so? – Shubham S. Naik Nov 10 '16 at 10:07
  • Because here result doesn't have memory(32* sizeof(unsigned int) bytes) in itself. It just point to same memory. So even if its scope is within the bin() function, the scope of memory pointed by result pointer which was previously created by malloc function is still until the program ends. – hrishi Nov 10 '16 at 10:48
  • Good. However you suggest 32 characters (bytes, 8-bit entities) but you allocate 32 `int` which are not easily handled by string functions. See my proposal below (with 33 chars which can both be manipulated as an array and printed easily as a regular C string). – Djee Nov 10 '16 at 11:25
1

Shubham, I really think you're going in the wrong direction as first there is no such thing as a decimal or binary integer. Let me suggest you to work on this which possibly solves your problem, using malloc and no static pointer which is not needed:

/*
 * return a 32-bit long binary string from 'integer'
 *  this string is dynamically allocated (malloc)
 * return NULL in case of error (so DON'T free it in this case)
 */
char* bin(long integer)
{
 char* pChResult=malloc(33*sizeof(char));
 char* pCh;
 unsigned long i;
 if(pChResult!=NULL) {
   for(pCh=pChResult, i=1<<31; i>0; pCh++, i>>=1) {
    if(i&integer)
     *pCh='1';
    else
     *pCh='0';
    }
   *pCh='\0';
  }
 return(pChResult);
}

and your main will be light, as well:

int main(void)
{
  unsigned int n;
  char* result;
  printf("Enter no:");
  scanf("%d",&n);
  result = bin(n);
  if(result==NULL)
   {
    fprintf(stderr, "internal error!\n");
    return(-1);
   }
  printf("binary representation is: %s\n", result);
  free(result);
  return(0);
}
Djee
  • 429
  • 3
  • 9
0

32-bit integral values impose to use signed long or unsigned long

In function bin just use *result all the time you need to store your result then return(result);

In main use *result as well and free(result); when you're done with the result

Also why not simply unsigned long bin(long);?

Finally, your function does absolutely nothing! Should be something like void bin(long, char[33]); (no malloc needed) or char* bin(long); (with a malloc)

Djee
  • 429
  • 3
  • 9
  • what is your opinion about the static pointer *result which I have declared in function bin(). It works even if I dont declare it static. But if its not declared static its class becomes automatic,and its scope will be inside the function bin() and as soon as function bin() finishes execution it will be destroyed correct? But still it works even if its automatic pointer. Why so? – Shubham S. Naik Nov 10 '16 at 10:10
  • Not quite. If `result` is a pointer to malloc'ed data, by `return(result);` you return the address of malloc'ed data. Memory location which retained this address in the scope of `bin` is lost but you don't care. What would be an error would be something like `return(&result);`. – Djee Nov 10 '16 at 10:44