buffbill wrote: |
---|
1 2
|
int a=6;
int binary[50] = {a};
|
Now all 50 elements in binary[] will hold 6 |
This is not correct. Only the first element will be set to 6. The other 49 elements will be set to 0.
GadgetRX wrote: |
---|
The first for loop counts the number of binary digits the base 10 number will produce. |
The first loop modifies the variable
b10 until the condition
b10 >=1
is no longer true.
The second loop's control condition is
b10>=1
. We know, because the first loop terminated, that when the second loops is reached,
b10>=1
has to evaluate to false, so the second loop will never be executed.
In the third loop the variable
e is initialized to the value of
newval, but newval was not initialized to any value and has never been set to any value, so it contains junk. The possibility of it containing junk that means the third loop is never executed is pretty high.
And finally,
int*binary = new int[a];
is equivalent to
int* binary = new int[0]
which makes binary point to an array of 0 elements. Accessing any elements of the binary array will result in undefined behavior.