I mean, WTF...

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
I mean, WTF...
by on (#40065)
Code:
if (CPU.A16 < 0x10)
   sprintf(Output, "A: 0x000%X", CPU.A16);

...won't work but...

Code:
if (CPU.A16 < 0x10)
   sprintf(Output, "A: 0x0%X", CPU.A16);


...just because I remove those two 0's. It puts the correct data into Output, but TextOut just returns 'The paramenter is incorrect.'.

I mean, WTF?

by on (#40066)
How big is Output?

And what are you doing when A16 >= 0x10? I have a feeling what you're trying to do in the sprintf has something to do with 0x%02X or 0x%04X.

by on (#40067)
Well I changed char *Output = ""; to char Output[100]; and now it works. Thanks anyway.

by on (#40072)
Attempting to write to a string literal is undefined behavior. Doesn't your compiler warn when you try to assign a string literal to a non-const char pointer? By being const-correct, the compiler can diagnose the error for you:
Code:
char* p = "foo"; // error in C++, and C compiler should warn
*p = 'b'; // will compile, but cause UB when executed
sprintf( p, "bar" ); // same, causes UB

const char* cp = "foo"; // OK
*cp = 'b'; // error because cp points to const object
sprintf( cp, "bar" ); // error because sprintf expects pointer to non-const char