printf() and dumping a char array

Apr 19, 2010 at 2:06am
Hi,

I have the following array defined:

unsigned char data[16];

After performing a fread and populating the data array I can successfully dump it's contents as hex to the console window. However, when I attempt to dump just the raw data the .exe crashes, I don't understand why.

1
2
3
4
5
for ( i = 0; i < size; ++i )
{
   printf("%02X ", data[i]); // Works.
   printf("%S", data[i]); // Crashes.
   ...


I've tried other things like: printf("%S", (char)data[i]); but without success.

What's the difference that it doesn't like string but will format as hex?

Thank you,
AJM.
Last edited on Apr 19, 2010 at 2:07am
Apr 19, 2010 at 2:32am
The problem is because data[i] is not a Cstring, it is a character, so when you attempt to print it as a Cstring, printf() simple acts like it is a char* and accesses basically random memory, causing a crash.
Last edited on Apr 19, 2010 at 2:32am
Apr 19, 2010 at 2:35am
Thanks firedraco, printf("%c", data[i]); fixed it :D

AJM.
Topic archived. No new replies allowed.