int fkt (int & i) {return i ++; }
int main() { int i = 5; printf("%d ", fkt(i)); printf("%d ", fkt(i)); printf("%d ", fkt(i)); }
prints '5 6 7'. Say I want to print "5 7 9" as follows: is it possible to do the same without a temporary variable in fkt ()? (A temporary variable will slightly decrease efficiency, right?) Ie, something like
return i+=2
or
return i, i+=2;
which first increments i and then returns it, which is not what I need.
thanks
EDIT: The main reason I do this is in a function, not the outside, because fkt will be a function pointer. The original function will do something else with i. I just feel like using {int temp = i; i + = 2; return temp;} does not look as good as {return i ++;}.
I don't need printf, this is just to illustrate the use of the result.
EDIT2: Wow, there seems to be more chat than a traditional board :) Thanks for all the answers. My fkt is actually that. Depending on some condition, I define get_it as get_it_1, get_it_2, and get_it_4:
unsigned int (*get_it)(char*&); unsigned int get_it_1(char* &s) {return *((unsigned char*) s++);} unsigned int get_it_2(char* &s) {unsigned int tmp = *((unsigned short int*) s); s += 2; return tmp;} unsigned int get_it_4(char* &s) {unsigned int tmp = *((unsigned int*) s); s += 4; return tmp;}
For get_it_1 it is so simple ... I will try to give more information in the future ...
source share