I try to return char array from function. I am new in C and try to learn function return value. This is my code:
int main()
{
unsigned int nr;
unsigned int mask=32;
char *outString;
printf("Enter Nr:\n");
scanf("%u",&nr);
outString = getBinary(nr,mask);
printf("%s",outString);
//getch();
return 0;
}
char * getBinary(int nr,int mask)
{
static char outPut[sizeof(mask)]="";
while(mask>0)
{
if((nr&mask)==0)
{
strcat(outPut,"0");
}
else
{
strcat(outPut,"1");
}
mask=mask>>1;
}
//printf("%s",outPut);
return outPut;
}
I can't make program work! With two error on function call.
char * getBinary(int nr,int mask);before yourmainoutPutarray (assumingsizeof(int)is 4 on your machine. You should make it larger. Also, you will see a problem if you callgetBinarymore than once, because the new number's digits will be appended to the old number's digits. You should setoutput[0] = '\0';before the loop.