[vox-tech] C-Newbie needs help with a source-code

Michael J Wenk vox-tech@lists.lugod.org
Thu, 30 Oct 2003 14:05:09 -0800


On Thu, Oct 30, 2003 at 12:16:51PM -0800, Nicole TWN wrote:
> At 06:06 AM 10/30/2003, you wrote:
> >what the program is about: you may input a decimal number and the program 
> >will convert it into a dual number... after this it will determine the 
> >length of the dual number (bestimme_laenge() ) then it should declare an 
> >array Dec[] with last position --> lange-1 then the positions Dec[i] ... 
> >i++ were filled in while while() after this it shut put out the positions 
> >of the array from Dec[lange] to Dec[0] so that the dual number is in the 
> >right order... but there are more numbers in the output the array shoul be 
> >long... o.O thats what i don't understand... i hope you can help me with 
> >this prob... greetz DaRkI sorry, my english is bad.. i know... but I hope 
> >you'll understand what i mean ;)
> 
> 
> OK, but you're not giving us a whole lot to work with here.  Let's break 
> this into smaller problems.
> 
> So: you need to convert a decimal number into a binary number, then fill a 
> variable-length array with the binary digits?  And you don't know how big 
> it's going to be?
> Now, the problem here is that plain C will not allow you to declare new 
> variables in the middle of a program (though C++ will).  Your question 
> seems to be: how do I know how big to make it?  I can think of two 
> different approaches:
> 1. Dynamic memory allocation.  At the top of your program, declare a 
> pointer (int *i) or an empty array (int i[]).  Once you know how big the 
> binary number is going to be, use malloc() to assign some memory to that 
> pointer.
> 2. Use your brain.  What's the biggest number a single integer variable can 
> store?  Why?  How is this related to the largest size your array will ever 
> be?  Hint: look up sizeof(int) on your computer.

Or combine them, if you are interested in portability.  Have the program
make an array ptr, and then malloc the array based on what sizeof(int)
returns.