I have int *b and a function
void to_Binary(int num, int range, int **bi_res)
{
int k = num, c = 0, r;
*bi_res = (int *) calloc(range,sizeof(int));
while (range >= c) {
r = k%2;
c++;
(*bi_res)[range-c] = r;
k /= 2;
}
return;
}
This is an example of how I send b to the function
void main
{
// A small example if you want to replicate.
int *b;
int i = 0;
to_Binary(56,6,&b);
while (i < 6) {printf("%d\n",b[i]); i++;}
return;
}
but it gives me seg fault in the loop
Note: The function itself works fine and does what it has to do if I access it in other ways but in the case I am required I have to return an array of ints that I can't know the size of
*(bi_res[range-c]) = r;. You should get a warning during compilation about this.whileloop instead offorloop?for (index = range-1; index >= 0; index--)and then used[index].