I am having trouble with unsigned char *. Here is my code:
unsigned char *str=NULL;
str= (unsigned char*) realloc(str, 10*sizeof(unsigned char));
int number=10;
str[0]=(unsigned char) number;
Whenever I try to see str[0] with cout, it shows something else other than 10. I also have trouble when putting other variable into str:
unsigned char c='c';
str[0]=c;
cout<<str[0];
str[0] would output a. What am I missing?
s[0] = 10;means put character code10into the first location of arrays. Try:Probably you would like instead to use the function
itoa(10, s, 10);See itoa manual, the function itoa is declared as…
If you try to do
cout << character; it will print the character, not the character code. To print the character code you should docout << (int)character;. Don’t useunsigned charfor characters, characters are, well, signed chars, simply chars, for friends.