by PudgePacket · 6y ago · 0 comments
> In this series, I'm trying something different. Let's take a grungy, optimized, pointer-type-punning, SSE-using, heap-eschewing, warning-disabling C program and look at how we could create the same program in Rust.
"In C, bodies[i] is exactly the same as *(bodies + i) — it performs pointer arithmetic and a dereference and nothing else. In particular, it assumes that you have some reason to know that i is a valid index for the array. This is the common case in C and gets a shorthand using square brackets."
In C, bodies[i] is not (bodies + i), with the exception being char arrays. The general rule is: bodies[i] = *(bodies + i * sizeof(bodies[0])) #include <stdio.h>
int main() {
int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
printf("%d %d\n", array[4], *(array + 4));
return 0;
}
prints "4 4".