First you need, for each integer, to know how many decimal digits it occupies. If you have at hand a logarithm function, better, else go multiplying a variable by 10
until she’s bigger than the number:
int numero = 1234;
int qtosDigitos = 1;
int limite = 10;
while ( numero >= limite ) {
qtosDigitos++;
limite *= 10;
}
Then just create the array with this size, and go picking the digits one by one:
int* digitos = (int*)malloc(qtosDigitos * sizeof(int));
for ( int i = 0 ; i < qtosDigitos ; i++ ) {
limite /= 10;
digitos[i] = (numero / limite) % 10;
}
Note: This solution only applies to non-negative numbers.
Now just iterate over the array and do this number by number:
int** digitosNums = (int**)malloc(tamanhoNums * sizeof(int*));
for ( int t = 0 ; t < tamanhoNums ; t++ ) {
int numero = nums[t];
... // Código acima
digitosNums[t] = digitos;
}
Note: as already explained in your other question, the C language offers no means of dynamically discovering the size of an array, so you need to store this information somewhere or use a null terminator when applicable. The above example does not do this, so be careful when adapting.
You want a character array (e.g..:
'1'
) or whole (ex.:1
)? I answered for whole, but it’s easy to adapt if you wantchar
s.– mgibsonbr
Even whole, it’s easier to manipulate.
– Leonardo