Note that there are some explanatory texts on larger screens.

plurals
  1. POReferencing a typedef as its struct counterpart
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p><strong>So here's the catch:</strong> 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: </p> <blockquote> <p>error C2440: '=' : cannot convert from 'ball *' to 'ball_struct'</p> </blockquote> <p>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:</p> <p><strong>The struct / typedef:</strong></p> <pre><code>typedef struct ball_struct { double mass; vector pos, vel, acc; /* keep list of neighbors and its size */ struct ball_struct *neighbors; int numNeighbors; } ball; </code></pre> <p><strong>And the erroneous function:</strong></p> <pre><code>/* adds ball reference to the neighbor list of the target */ void addNeighbor(ball *target, ball *neighbor) { int n = target-&gt;numNeighbors; target-&gt;neighbors[n] = neighbor; // error C2440 target-&gt;numNeighbors = n+1; } </code></pre> <p>Thanks, any help is appreciated. <em>Remember, only solutions for C please.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload