title: C/C++: goto into the for loop
body: <p>I have a bit unusual situation - I want to use goto statement to jump <strong>into</strong> the loop, not to jump out from it.</p>
  
  <p>There are strong reasons to do so - this code must be part of some function which makes some calculations after the first call, returns with request for new data and needs one more call to continue. Function pointers (obvious solution) can't be used because we need interoperability with code which does not support function pointers.</p>
  
  <p>I want to know whether code below is safe, i.e. it will be correctly compiled by all standard-compliant C/C++ compilers (we need both C and C++).</p>
  
  <pre><code>function foo(int not_a_first_call, int *data_to_request, ...other parameters... )
  {
      if( not_a_first_call )
          goto request_handler;
      for(i=0; i&lt;n; i++)
      {
          *data_to_request = i;
          return;
  request_handler:
          ...process data...
      }
  }
  </code></pre>
  
  <p>I've studied standards, but there isn't much information about such use case. I also wonder whether replacing <strong>for</strong> by equivalent <strong>while</strong> will be beneficial from the portability point of view.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPD</strong>: Thanks to all who've commented!</p>
  
  <ol>
  <li><p>to all commenters :) yes, I understand that I can't jump over initializers of local variables and that I have to save/restore <code>i</code> on each call.</p></li>
  <li><p>about strong reasons :) This code must implement reverse communication interface. Reverse communication is a coding pattern which tries to avoid using function pointers. Sometimes it have to be used because of legacy code which expects that you <strong>will</strong> use it.</p></li>
  </ol>
  
  <p>Unfortunately, r-comm-interface can't be implemented in a nice way. You can't use function pointers and you can't easily split work into several functions.</p>