Ok guys, we all know there are all lot of typedef/struct questions out there, but I feel this one is a bit of a mind bender.
I'm simulating the neighbor interactions of a crystal lattice using strictly C. I have a struct called "ball_struct" which I typedef'ed as just "ball". The struct contains a pointer to a list of ball_structs (since I couldn't use the typedef name before its own declaration) that the ball considers its neighbors.
So here's the catch: I want to add balls to this list of list of ball_struct neighbors. When I compile (in Visual Studio 2009, no CLR support) I get:
error C2440: '=' : cannot convert from 'ball *' to 'ball_struct'
I'm not surprised, but I am stumped. Is there a way to cast the typedef back down to its respective struct? If not, is there anyway I can add a "ball" to a "ball_struct" list so I don't have to remove the typedef and paste "struct" keywords all over my code? Here's the code in question:
The struct / typedef:
typedef struct ball_struct
{
double mass;
vector pos, vel, acc;
/* keep list of neighbors and its size */
struct ball_struct *neighbors;
int numNeighbors;
} ball;
And the erroneous function:
/* adds ball reference to the neighbor list of the target */
void addNeighbor(ball *target, ball *neighbor)
{
int n = target->numNeighbors;
target->neighbors[n] = neighbor; // error C2440
target->numNeighbors = n+1;
}
Thanks, any help is appreciated. Remember, only solutions for C please.