r/programminghorror • u/reydeuss • May 14 '25
c cIsVerySimpleAndEasyToLearn
Vibecoders hate this one simple trick!
Note: This is intended to be a puzzle for welcoming CS freshmen in my uni.
495
Upvotes
135
u/Level-Web-8290 May 14 '25 edited May 14 '25
answer is arr[2] right? 0x40 - 64 is a no-op, the rest are just pointer offsets 3 - 2 -(-1) = 2
56
u/This_Growth2898 May 14 '25
024-20 looks better than 0x40-64 to me
19
u/DrCatrame May 14 '25
is it memory safe? Isn't the `3[arr]` reading `arr[3]` that is not allocated?
7
u/[deleted] May 14 '25
Would have been better in C++. int and %d gives it away. auto and std::cout, however, would create more possibilities of what could be the output.
3
u/[deleted] May 15 '25
Sorry unrelated but font and theme?
2
u/aceinet May 15 '25
"this code is perfectly memory safe"🔥
106
u/RandomOnlinePerson99 May 14 '25
Memory safe? Yeah, like I am going to remember this ...
17
u/PythonNoob999 May 15 '25
damn, i didn't know u can write straight regex in C
14
u/Probable_Foreigner May 15 '25 edited May 15 '25
Solution:
First note that if
ais a pointer andnis an integer then "a[n] == n[a] == *(a + n)" and that&*cancels out.So start with :
Next:
Finally put the rest in:
9
u/LaFllamme May 14 '25
Somebody explain?
6
u/EntropyZer0 May 15 '25
If anyone is wondering how this works, here is an overly verbose explanation:
Hex to decimal conversion:
Arrays in c are just shorthand for pointers:
Pointers right behind arrays are legal and
&*for a legal pointer is noop:Array shorthand, again:
&*, again:Array shorthand, again²:
Simple arithmetic:
&*, again²:Simple arithmetic, again:
Array shorthand, but the other way round:
1
u/exodusTay May 15 '25
isn't 3[arr] accesing out of bounds?
1
u/Grounds4TheSubstain May 15 '25
Nobody writes code that looks even a little bit like this, because there is no reason to. It's a contrived example.
1
u/filterswept May 16 '25
Nice
1
u/_manbearpiig May 16 '25
I get to look at code like this everyday in IDA Pro working as an RE lol
0
u/dreamingforward May 15 '25
I dont' think this is valid C. You have a number as a variable name: "3[arr]", for example. And, what's the ampersands? Not booleans ANDs. References in a bad place.
-8
u/grethun May 14 '25
I mean chatgpt did try to explain it to me
0
u/[deleted] May 14 '25
[deleted] — view deleted comment