Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As both answers already stated, the problem is that <code>Q-&gt;current_value</code> has never been assigned a value to and so it points to an undefined address and every dereferencing like <code>*Q-&gt;currentValue = ..</code> is undefined behaviour. The fact that code 1 seemingly works doesn't prove anything else because due to the nature of UB no behaviour is guaranteed, your program may or may not crash (or your dog may explode, dragons fly out of your nose ... :-) )</p> <p>Of course there are multiple solutions that all mean something different:</p> <ol> <li><p>if <code>currentValue</code> should just hold a copy of a certain value, it could be <code>int currentValue</code> instead of <code>int *...</code> and the assignnment would be</p> <p>Q->currentValue = *Q->theQueue;</p> <p>and the return statement would be <code>return &amp;Q-&gt;currentValue</code>. In that case you would return a pointer to the <strong>original</strong> value of <code>theQueue[0]</code></p></li> <li><p>if you want to point to the location in <code>theQueue</code>, Jim's anser tells the correct way:</p> <p>Q->currentValue = Q->theQueue;</p> <p>In that case you yould return a pointer to the <strong>new</strong> value of <code>theQueue[0]</code> (which may be what you don't want)</p></li> <li><p>you could allocate memory to <code>Q-&gt;currentValue</code> my <code>malloc( sizeof (int) );</code> and then leave the assignment as it is. In that case you would return a pointer to the <strong>original</strong> value of <code>theQueue[0]</code> like in (1)</p></li> </ol>
 

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