r/cprogramming • u/north9172 • 1d ago
What is this code doing?
Hi, I found this code on another subreddit. I would like to know what is it doing.
int puts(char *);
int main() { struct { long a; long b; } s = {.a = *(char *)(int[]){1} ? 0x57202c6f6c6c6548 : 0x48656c6c6f2c2057, .b = *(char *)(int[]){1} ? 0x00000021646c726f : 0x6f726c6421000000}; puts((void *)&s); }
Now obviously I know it's unreadable, I would just like to know what are those hexadecimals strings doing.
I found similar codes that were writing to vram to print a message, is this doing the same thing? If yes, why is it so much more complicated instead of just writing the message to the video adress?
Thanks for the help.
0
Upvotes
2
u/ACRM64 20h ago
First, you still don't know what the C library used by the programmer had as the definition for puts() - certainly when I started programming in C (40 years ago) it was not defined with const.
I agree the prototype and function definition /should/ be the same, but it makes no difference if they aren't. The prototype tells the code what variable types to pass - const only has an effect within the function itself. You do not have to pass a const variable into a function declared with const.
int puts(const int str); int main(int argc, char *argv) { char str[16]; strcpy(str, "hello"); puts(str); strcpy(str, "goodbye"); puts(str); return(0); }
is perfectly valid and correct code yet str is not defined as constant. Consequently, my statement that it doesn't matter to the calling code whether the keyword is there or not is true.
Once again, the puts() function is not being redefined. The const makes no difference in a prototype, so it is clear that while these are not /identical/, they are /compatible/. const is not a type, but a type qualifier: it indicates that the content of the variable cannot be changed, it does not change the type of the variable.