Guys i have next two pieces of code which extracts 32bit variable from 8bit 1) First is that:
#include <stdio.h>
#include <string.h>
int main()
{
unsigned char buffer[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee};
unsigned char *buff = buffer;
unsigned int result;
result = *buff++;
result += *buff++ <<8;
result += *buff++ << 16;
result += *buff++ <<24;
printf("result = 0x%x, *buffer = 0x%x.", result, *buff);
return 0;
}
Without any warning, but it looks a little lame....
2) In second we have macro instead of those ugly 4 lines:
#include <stdio.h>
#include <string.h>
#define to32(buffer) ((unsigned int)*buffer++ | *buffer++ << 8 | *buffer++ << 16 | *buffer++ << 24)
int main()
{
unsigned char buffer[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee};
unsigned char *buff = buffer;
unsigned int result = to32(buff);
printf("result = 0x%x, *buffer = 0x%x.", result, *buff);
return 0;
}
And it leaves next warning:
main.cpp: In function 'int main()':
main.cpp:4:99: warning: operation on 'buff' may be undefined [-Wsequence-point]
#define to32(buffer) ((unsigned int)*buffer++ | *buffer++ << 8 | *buffer++ << 16 | *buffer++ << 24)
And i'm little confused what's exactly GCC found as undefined behavior. Is it that all shifts in one line and i sum it?