-
returning an array
//This code is supposed to accept a decimal integer and print out the number in binary by calling a function that returns the binary number in an array//
Code:
#include <stdio.h>
#include <stdlib.h>
char *int_to_binary(int x);
int main()
{
int dec;
printf("Please enter an integer/n");
scanf("%d", &dec);
int_to_binary( dec );
printf("%s\n", int_to_binary(17));
return 0;
}
char *int_to_binary(int x)
{
int a;
char bin[17];
int_to_binary=bin;
for( a=0; a<17; a++){
if (x%2==0)
{
bin[a]='0';
}
else
{
bin[a]='1';
}
x=x/2;
}
return * int_to_binary;
}
my compiler is telling me that I can't use my int_to_binary pointer to point to my bin array. How else can I use the pointer to return an array to main? Please HELP thank you!
-
Define the array in main. Pass it to int_to_binary as a parameter, where it will be filled in.
-
Is there any real benefit to return an array?
-
int_to_binary is the name of the function, so in this line you are trying to assign a char pointer to an immutable function pointer. you don't need to do that and it won't compile.take that line out, do what oogabooga said and return the input parameters as the output.