C / C ++ Post-increment of more than one

I read bytes from the buffer. But sometimes what I read is a word or longer.

// assume buffer is of type unsigned char * read_ptr(buffer+(position++)) 

This is great, but how can I place a position adjusted for 2 or 4? I can't force the operator += post-increment, right?

The reason is that I have this big terrible expression that I want to appreciate, while at the same time increasing the position variable.

I think I came up with my solution. I am sure it works. However, everyone will hate it because it is not very readable code.

 read_ptr(buffer+(position+=4)-4) 

Then I will do it in the macro, checking it a bit to make sure that it is doing the right thing.

IN CUSTODY:

Do not do this. This is just a bad idea because it is what code that generates indescribable code. But ... it turned out that converting any pre-incremental statement into a post-incremental process is pretty easy.

+4
source share
7 answers

Well, I answered my question in the editorial office ... Basically, I wanted to have one expression that evaluates the original value, but has the side effect of incrementing by an arbitrary amount. Here are a few macros.

 #define INC(x,inc) (((x)+=(inc))-(inc)) #define INC2(x) INC(x,2) #define INC4(x) INC(x,4) #define INC8(x) INC(x,8) 
0
source

how can i plot position to 2 or 4?

You cannot add a variable value to 2 or 4, but you can use the following (in your case)

read_ptr(buffer+position); position += 2;

+3
source

The + = operator is a separate operator (not post. Or pre increment). You can use the following line:

 func(buffer + position); position += 2; 
+3
source

You do not do this; you break it into several lines. There is no reason to put everything on one line.

 read_ptr( buffer + position ); position += n; 
+3
source

Although, I would not recommend this solution, but if you do not want to change this line in your code:

 read_ptr(buffer+(position++)); 

And you still want the post-increment position to be 2, and then define the position as Index position(2); where the Index type is indicated here, and usage is also shown:

 struct Index { int step; int value; Index(int s=1, int v=0): step(s), value(v) {} Index operator++(int) { Index prev(step, value); value += step; return prev; } operator int() { return value; } }; int main() { char arr[] = "1234567890" ; cout <<"Increment by 2" <<endl; Index i2(2); //increment by 2 cout << *(arr + (i2++)) << endl; cout << *(arr + (i2++)) << endl; cout << *(arr + (i2++)) << endl; cout << *(arr + (i2++)) << endl; cout <<"Increment by 3" <<endl; Index i3(3); //increment by 3 cout << *(arr + (i3++)) << endl; cout << *(arr + (i3++)) << endl; cout << *(arr + (i3++)) << endl; cout << *(arr + (i3++)) << endl; return 0; } 

Output:

 Increment by 2 1 3 5 7 Increment by 3 1 4 7 0 

Working example: http://ideone.com/CFgal

Note: I would not offer this solution in real life. This is more like a puzzle: D

+3
source

If position was a pointer to int16 or int32 , then incrementing it would add 2 or 4, respectively.

+1
source

In C ++, you can easily write a function to do a double increment after a style:

 template <typename T> T inc2(T &t) { T r(t); ++t; // or t++ if you want to respect inconsistently-overloaded operators, ++t; // but I wouldn't bother. return r; } read_ptr(buffer+inc2(position)) 

In C, this is a bit more inconvenient:

 size_t inc2(size_t *s) { // or whatever type you're using size_t r = *s; (*s) += 2; return r; } read_ptr(buffer+inc2(&position)) 

You can also cover case 4 by making it an additional functional parameter or perhaps an additional template parameter in the case of C ++.

There is another question: is it worth using this style of programming in C ++ or C, where you do so much in one statement. Avoiding side effects can make the code more understandable, even if it is longer.

+1
source

Source: https://habr.com/ru/post/1344352/


All Articles