The problem is that you've created num_array
as a local within convertNumberToArray()
and later you return the address of that variable -- once you return from convertNumberToArray()
all of its locals are no longer valid. What happens when you do this is undefined. When you see the correct results, you've just gotten lucky; none of the data is safe though. You must never (ever ever ever...) return the address of a local variable expecting the data it points at to remain in tact.
To solve this correctly, create the array to return data to within main()
and pass it to the function as a new parameter. An alternative is to use malloc()
(or any other allocation function) to create the buffer on the heap within convertNumberToArray()
but if you do this, you must also write code to return the memory later (or you will cause a memory leak).