Hacker News new | ask | show | jobs
by sgtnoodle 1900 days ago
Are those not equivalent expressions in modern C?

I imagine there are more optimal and less optimal ways of actually doing the indexing in machine code and the former may be better semantics, but I would think a compiler would generate identical machine code for both.

1 comments

I’m pretty sure you have to take the size of the objects in mind.
No, these are equivalent.

The size of the objects is implicitly taken into account by the compiler, it knows the size of the objects by the type of the pointer.

"Pointer arithmetic" takes care of that. Adding an integer to a pointer will multiply the size of the type pointed to by the integer and adds that to the pointer.