简体   繁体   中英

Why the pointer from called function doesn't return the value to calling function?

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

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;
}

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);
}

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 )

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