Very quick/simple question

May 24, 2011 at 4:36pm
I apologize for such a mundane question, but it has been while since I've coded. What does the following code do:
1
2
3
4
const BYTE* buff,	/* Data to be sent */
      BYTE d;
		
      d = *buff++;



I understand declaring buff as a BYTE pointer, and d as a byte, but ++ is throwing me.

Thanks!
May 24, 2011 at 4:44pm
The ++ part is incrementing buff by one (BYTE). The * is de-referencing the value to which buff pointed before incrementing.
May 24, 2011 at 9:13pm
Awesome, thank you!
May 24, 2011 at 10:43pm
I strongly recommend creating a test program for this and investigating the output:
1
2
3
4
5
6
7
8
int n = 10;

const int * buff = &n;
int d = 0;

std::cout << "buff = " << buff << " n = " << n << " d = " << d << std::endl;
d = *buff++;
std::cout << "buff = " << buff << " n = " << n << " d = " << d << std::endl;

Last edited on May 24, 2011 at 10:47pm
May 24, 2011 at 11:06pm
I also recommend you don't write confusing code like that.

This code:
1
2
d = *buff;
++buff;

works just as well and is less confusing. Just because you can do it in 1 line doesn't mean you should.
Last edited on May 24, 2011 at 11:07pm
Topic archived. No new replies allowed.