Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Char Array not showing correctly

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
Looks like this is my week for questions, most of them probably have really easy answers.

I have allocated a char array as follows

char mArr[4] = {'0','0','0','0'};

itoa(data,&mArr,10);

But when it is displayed it shows a load of garbage.

When I display it to the screen, it shows 0002IIIIIIIITM/
I was expecting the 0002, but the rest is just garbage, and I don't see where it has come from when the array is explicity declared as 4 bytes, and data is a single 16 bit integer.

I have tried rewriting the code with a '\n' for the end of string, and making the array 5 cells, but it makes no difference.

I apologise if this is a real noddy question, but I usually use std::string, but want to use char[] here, and need to understand what I am doing wrong.

TIA,

K
 
Your mArr can only hold 3 chars + terminating zero. But where the zero should be you put a '2'. Hence the string is printed up to the first \0 it encounters.


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Cheers, I'll try that later and fix it.
I new it would turn out to be something simple.

Do you live in here Per? You seem to answer everything going very quickly! :)
 
>Do you live in here Per?

Nah, I just check in every now and then while compiling.

>You seem to answer everything going very quickly! :)

Well, I compile a lot :)

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top