0

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.

5
  • 1
    what error you get?? Commented Nov 14, 2016 at 10:18
  • Implicit declaration of function. And assignment make pointer rom integer with out a casting. Commented Nov 14, 2016 at 10:19
  • 3
    Try to put a prototype char * getBinary(int nr,int mask); before your main Commented Nov 14, 2016 at 10:19
  • thank, works perfect. Commented Nov 14, 2016 at 10:24
  • @AliAbdulhussein: Well, it probably works as long as you put no more than 3 digits in the outPut array (assuming sizeof(int) is 4 on your machine. You should make it larger. Also, you will see a problem if you call getBinary more than once, because the new number's digits will be appended to the old number's digits. You should set output[0] = '\0'; before the loop. Commented Nov 14, 2016 at 10:36

2 Answers 2

2

The major problem is, sizeof(mask) is not doing what you think it does. This is equivalent to sizeof(int) which is not what you want there.

You should better stick to a pointer and memory allocator function for this purpose.

FYI, you don't see issues currently with

 static char outPut[sizeof(mask)] "";

as sizeof is a compile time operator, so this outPut is not VLA. As soon as you try to change it to

static char outPut[mask] = "";

you'll face problems, as

  • VLAs are local scope and incomplete type, static storage is not allowed.
  • you cannot initialize VLAs.

Also, you must provide the prototype (forward declaration) to getBinary() if you intend to define it after main().

Sign up to request clarification or add additional context in comments.

6 Comments

Returning a local variable(array) outPut leads to UB even if it is static?
His problem is he doesn't have a prototype for the function getBinary()
@Gopi there's a bigger one, hidden,
@Gopi also, what makes you think that sizeof(mask) is not an issue?
When did I say it is not an issue?
|
0

you can change program like following:

#include <stdio.h>
#include <string.h>
char * getBinary(int nr,int mask); // add function header, it necessary to avoid compilation error 
//otherwise you can move getBinary function before your main function, because the compilator cannot recognize your function when it is defined after the call.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.