I have a variable that I pass into a function. in that function I have an itoa() to convert it into an array
ex.
MyVar = 5;
Convert(MyVar);
void Convert(int NewVar){
char myArray[4] = {0};
itoa(NewVar,myArray,2);
....
}
I have no errors or issue EXCEPT ONE!
If I multiply MyVar afterwards by '10' I get a weird number. so MyVar*10 suppose to be 50, but instead it's 280. I can multiply it with any number and i'll be fine except '10'.
I traced it down to the 'itoa' function (commenting it out gave me the correct answer).
I have no clue as to why it's doing this, for the mean time I'm forced to use MyVar52, which works.
This is not your complete code. This code produces a compilation error.
NEW:23: error: 'BinHolder' was not declared in this scope
char* Bstring = BinHolder + 1;
^
exit status 1
'BinHolder' was not declared in this scope
There's no point posting code that doesn't compile and asking us to work out why it does weird things when it runs. Not only does this code not compile, it's not the code that you originally posted.
I note that in your code, you do not add a line break after the code in BinV that prints the digits. So that "280" could very well be two numbers smooshed together in your output. I also note that the loop that prints the digits does not check for a trailing '\0'. This means that unless the result of itoa is exactly four bytes, you will get the '\0' printed out followed by some garbage on the stack. Probably.
Assuming that 'BinHolder' is meant to be 'Holder', I would expect this code to print out:
01<'\0' - an invisible character >50
Other issues include:
1 - Usually we use the word 'buffer' rather than 'holder'
2 - Why would you want to do that? I mean - why use itoa to get the bits in a numeric value (which is almost certainly what you are doing)? Normally you'd use the C bit-fiddling operations. The problem with itoa is that the length of the result depends on what the value acually is, tha length is not consistent because 0 is a special case, and it's just unnecessary.
agree with PaulMurrayCbr bet on where the error is:
You are attempting to represent an integer that is likely greater than 16 (assuming Y was positive in the function since you send 5 in) in binary (base 2)
char Holder[4] = {0};
Z = Y+16;
itoa(Z,Holder,2);
and hope it fits on 3 characters?
16(base 10) is already 10000(base 2) so needs 5 characters plus the trailing '\0' for a c-string...
sorry for the typo in the code. seeing how itoa is giving me such trouble for such a simple function. I've decided to scrap it and just to do an 10x4 array to hold the binary values for 0-9.
No more errors. I still don't know how that function could affect the variable like that.
Note
The minimal size of the buffer depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
That means your buffer should have been 17 characters. The function assumes you provided a sufficient buffer and when you don't, it writes into memory that is adjacent to your buffer. At that point all bets are off and anything can happen.
dilldoe:
sorry for the typo in the code. seeing how itoa is giving me such trouble for such a simple function. I've decided to scrap it and just to do an 10x4 array to hold the binary values for 0-9.
No more errors. I still don't know how that function could affect the variable like that.
Dude, there's just no question that there's absolutely no need to do this. No-one working in C would ever need to do a thing like this. C does this sort of stuff natively as part of the language - you don't need to write some sort of complicated mechanism to work out whether bit 3 of the binary representation of 10 is a one or a zero.