Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I increase int i by more than one using the i++ syntax?
    text
    copied!<p>int fkt(int &amp;i) { return i++; }</p> <pre><code>int main() { int i = 5; printf("%d ", fkt(i)); printf("%d ", fkt(i)); printf("%d ", fkt(i)); } </code></pre> <p>prints '5 6 7 '. Say I want to print '5 7 9 ' like this, is it possible to do it in a similar way without a temporary variable in fkt()? (A temporary variable would marginally decrease efficiency, right?) I.e., something like </p> <pre><code>return i+=2 </code></pre> <p>or </p> <pre><code>return i, i+=2; </code></pre> <p>which both first increases i and then return it, what is not what I need.</p> <p>Thanks</p> <p>EDIT: The main reason, I do it in a function and not outside is because fkt will be a function pointer. The original function will do something else with i. I just feel that using {int temp = i; i+=2; return temp;} does not seem as nice as {return i++;}. </p> <p>I don't care about printf, this is just for illustration of the use of the result.</p> <p>EDIT2: Wow, that appears to be more of a chat here than a traditional board :) Thanks for all the answers. My fkt is actually this. Depending on some condition, I will define get_it as either get_it_1, get_it_2, or get_it_4:</p> <pre><code>unsigned int (*get_it)(char*&amp;); unsigned int get_it_1(char* &amp;s) {return *((unsigned char*) s++);} unsigned int get_it_2(char* &amp;s) {unsigned int tmp = *((unsigned short int*) s); s += 2; return tmp;} unsigned int get_it_4(char* &amp;s) {unsigned int tmp = *((unsigned int*) s); s += 4; return tmp;} </code></pre> <p>For get_it_1, it is so simple... I'll try to give more background in the future...</p>
 

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