I have an array of structs, which is dynamically allocated. A pointer to this array is passed around to other functions.
struct body{
char* name;
double mass;
// ... some more stuff
};
body *bodies = malloc(Number_of_bodies*sizeof(body));
I need to know the size of the array, so I'm storing the size in one of the structs, which is in the 0th element of the array (the first struct).
bodies[0].mass = (double)Number_of_bodies;
I then return from the function a pointer to the 1st element of the array i.e bodies[1]
return (bodies+1);
Now, when I use this pointer in other functions, the data should start at the 0th element.
body *new_bodies = (bodies+1); //Just trying to show what happens effectively when i pass to another function
new_bodies[0] = *(bodies+1); //I Think
If I want to see the initial struct, which was at bodies[0]
, does that mean in other functions I have to access new_bodies[-1]
?
Is this something I can do? How can I access the initial struct?