0

Do all variables in a struct share a continous memory like arrays ?

I have seen a piece of code similiar to the one below and I was surprised as it was the first time I have seen such thing. sturct Vector3D { float x, y, z; float operator[](int i) { return ((&x)[i]); } } It works as if x was a starting point of an array and the rest (y, z) are the other elements in the array. I tried printing the addresses of x, y, z and I saw that they are stored like an array. How this works?

27th Jul 2020, 9:08 PM
Mustafa K.
Mustafa K. - avatar
2 ответов
0
Martin Taylor ops, i have written the code wrong, it had to be array access operator. edit: My point is the way the elements are accessed, I mean this line, return ((&x)[i]); how do x, y and z occupy a continous memory is it becase the codes are executed from top to bottom? I thought they might share different locations in ram in some cases, but since the current is so fast it doesnt let this happen when trying to put the variables to the free memory locations?
28th Jul 2020, 7:14 AM
Mustafa K.
Mustafa K. - avatar