I'll take you through the steps. Here's your code:
do {
int i = 0;
step1[i] = num % 10;
num /= 10;
i++;
} while (num != 0);
First, an error. The i
variable should be initialized and declared outside the loop, otherwise i == 0
every single time you go through the loop.
int i = 0;
do {
step1[i] = num % 10;
num /= 10;
i++;
} while (num != 0);
This should be recognizable as the expanded version of a for
loop, so:
for (int i = 0; num != 0; i++, num /= 10)
step1[i] = num % 10;
// note: different for case num == 0
Then, if you want to turn it into a function,
void get_digits(int digits[], long long num) {
for (int i = 0; num != 0; i++, num /= 10)
digits[i] = num % 10;
}
A long story short, when you pass an array to a function in C, the array doesn't get copied so you can change the array inside the function and see those changes outside the function. (Long version: when arrays are function parameters they decay into pointers.)
Final note: you will need 19 elements in the array, not 10. So:
long long num = 378282246310005;
int step1[19];
get_digits(step1, num);