Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Initializing a pointer (or array) <code>A * a</code> inside a method is a particular case of modifying your variable <code>a</code> (which is a pointer/array in this case). Modifying a variable inside a method can be done either by passing a pointer to it, or a reference to it. So in the case your variable is already a pointer, these two options are the following.</p> <p>By passing a pointer to your pointer <code>a</code>:</p> <pre><code>void init(A * *a) { *a = new A(); // or "new A[n]" if "a" is an array } void main() { A * a; init(&amp;a); } </code></pre> <p>Or passing a reference to your pointer:</p> <pre><code>void init(A * &amp;a) { a = new A(); // or "new A[n]" if "a" is an array } void main() { A * a; init(a); } </code></pre> <p>So in your case, <code>a</code> is an array of <code>unit*</code> (which I think is a bad design by the way, it would be better to simply use an array of <code>unit</code> and save you from this loop to dynamically allocate separately all units), so basically <code>a</code> is of type <code>unit **</code> so your <code>A</code> is actually the type <code>unit*</code>. Using the second method would lead to:</p> <pre><code>void init(unit* * &amp;units) </code></pre> <p>But as I said, it is probably better to simply use an array of <code>unit</code> instead, and your whole code would look like:</p> <pre><code>void initialize(unit * &amp;units /* etc */) { // note that I use unit instead of unit* here units = new unit[n]; for(register int i = 0; i&lt;n; i++) { // note that I remove the "new unit;" here // note that I use "." instead of "-&gt;" units[i].hunger = 5; units[i].locationX = (rand()%((grids-&gt;gridWidth)-0)); units[i].locationY = (rand()%((grids-&gt;gridHeight)-0)); //etc, etc } } void main() { unit * units; initialize(units); } </code></pre>
    singulars
    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.
    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