Question:
Help with a variable length int array in C?
Tony
2012-11-29 18:36:38 UTC
I'm trying to convert a binary number into a decimal number. I have an algorithm that works for smaller numbers because I store the input as a long long int. Unfortunately, that isn't enough for a larger binary number. I think the best way to store it is as an array, but I don't really know how to use variable length arrays. Can anyone show me how to store the input and determine when I've reached the end of the array? I need the end of the array so I can start doing calculations on it. I'll probably reverse the array to start at the end and work to array[0].
Three answers:
Steven
2012-11-29 19:26:43 UTC
I posted a link to a code I wrote -- perhaps the for loops with 2 counters is what you're looking for?



you can set a 1d array to be like this array[]; and then use the strlen() function to determine how long the input is.
thibeault
2017-02-26 20:06:50 UTC
there is none merchandise in C++ like length in Java. In C++ you need to declare the size of a typical variable till now applying it. char string[10]; //to illustrate To get the size of an array: type array[int measurement]; int length_foo = sizeof( array ) / sizeof ( type ); be careful, in case you have a project like this: type array [4]; array=[a million,2,3]; the size of the array would be 4, no longer 3
?
2012-11-29 20:40:16 UTC
You use a pointer and later define the array size with malloc. You will need to track the size with a second variable.



http://www.cplusplus.com/reference/cstdlib/malloc/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...