This is the first time that I use Stack Overflow to make a question. I created this code to divide the digits of a number in arrays:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int *getArray(int number);
int main(void)
{
int *x = getArray(1234);
printf("%i", x[0]);
}
int *getArray(int number)
{
int n = floor(log10(number)) + 1;
int *receive = calloc(n, sizeof(int));
for(int i=0; i<n; i++, number/=10)
{
receive[i] = number%10;
}
return receive;
}
It worked very well for integers but I tried to use it for large numbers and it didn’t work (I just changed some ‘int’s to ‘double’):
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double *getArray(double number);
int main(void)
{
double *x = getArray(100);
printf("%f", x[0]);
}
double *getArray(double number)
{
int n = floor(log10(number)) + 1;
double *receive = calloc(n, sizeof(double));
for(int i=0; i<n; i++, number/=10)
{
receive[i] = number%10;
}
return receive;
}
Does anyone know what I’m doing wrong? Thanks.