Ari Myr is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.

I did this out of pure dumb luck and now I don't understand how that division works or if it's even okay

This is how I'll actually become a programmer: out of pure, RAW luck

@arisunz by everything I know about C this should print exactly nothing

@arisunz if your architecture only has 8-bit pointers then this output makes perfect sense

@jordyd Shit, maybe. I'm on a 32-bit machine.

Don't ask why, it's sad.

@jordyd So the non-fucky way of doing this would be:

i < sizeof(menu) / sizeof(menu[0]),

right?

@arisunz actually `i < sizeof(menu)` should work. C treats static arrays differently than pointers, and reports the size as the number of elements

@arisunz actually I’m wrong it uses the total size

@arisunz I think what we’re seeing here is what C programmers call “black magic”. It works, ... for now. Correct way to do this would be to make the last element in the array `NULL` and then use `int i = 0; while (menu[i]) { printf("%s", menu[i]); i++ }`

@arisunz oh if you got a segfault then everything makes sense

@arisunz imagine I put a semicolon after the `i++`

@arisunz (sizeof(char*) gives the size of a pointer, which is 4 on 32 but machines, btw)

@arisunz also note that sizeof(char*) == sizeof(void*) == sizeof(anything*)

@jordyd Thank you for the explanations! I really need to watch my way around pointers...

Ari Myr @arisunz

@jordyd and basically do NOT rely in any way shape or form on anything that could be platform-specific

· Web · 0 · 0

@arisunz well, C is not great for cross-platforminess :)

@arisunz it’s a good idea to understand C, and then never touch it again if you can avoid it

@jordyd Yeah I'm diving right into Rust after I'm ""done"" with C

@arisunz If C lets you shoot yourself in the foot, Rust starts screaming if you so much as look at a gun

@jordyd I kinda like that about it, I feel like Rust forces you to learn elegant, thought out coding styles and practices from the get go

though I also like a loaded, unsafe shotgun as much as anyone

@jordyd also I ugly laughed at that xD