http://ideone.com/MKmT2 Can anyone help me with the TODO part of the repr_array procedure, which is to make it work for larger integers? How do I convert integers to their respective ASCII representation, and then write those ASCII representations into the string representation of the input array, so that repr_array works for a larger range of integers?
Can't spend too much time on it, but your ideal scenario is probably a combination of mod and integer division. In particular, remember that dividing an integer by an integer in C does not give you a floating point result. So if I divide 3 / 2, I actually get the integer 1, not 1.5. Then remember that mod gives you the remainder of a division. Together, you can do remainder division easily. In this particular case, some examples that might elucidate how this can be useful: 1359 / 10 = 135 1359 % 10 = 9 135 / 10 = 13 135 % 10 = 5 13 / 10 = 1 13 % 10 = 3 1 / 10 = 0 1 % 10 = 1
A little bit of looping should then let you achieve what you're looking for ;)
Though again, recall what I've mentioned before: you want to try to make sure you malloc and free in the same function. Architecturally speaking, this would mean making repr_array take a char* in which it should be placing content, rather than returning something malloc-ed in repr_array.
Your idea is simple and works great; I was thinking of some complicated strncat, sprintf, and writing my own itoa() function, but your idea will probably work more effectively thanks Also, thanks for reminding me of the good practice of placing malloc responsibility on the caller
Here's what I've done: http://ideone.com/xTbwI is it alright or is there a huge problem lurking in there?
Mmm. sprintf. So handy. That said, you should watch out. I'm pretty sure you're severely under-allocating space (i.e., it looks like you're leaving 1-2 bytes of memory for each entry in the array, but what if one entry is 65000? Then you'll need 5 bytes + the comma).
Join our real-time social learning platform and learn together with your friends!