Originally Posted by
shaileshk
Yes, but then if its '1234' then why it is falling at the point when its fliping the value i.e. TempBytePtr[0] = TempBytePtr[1];
And when I comment this piece of code it do work correctly so i have doubt that the fliping is not working due to some restrictions.
That whole function should not even compile. The compiler should tell you you are doing something very wrong and not create an executable. If it does not, you should try a different compiler.
Although an int may be four bytes (and, presumably, a "Uint16" is two bytes), that does not mean they are arrays. You could have an array of Uin16:
In this case, eg[0] would be a two byte Uint16. If you only have one:
There is no such thing as eg[0] or eg[1]. There is only eg.
If you want to swap the bytes around in a multi-byte datatype, you need to use a union (make sure you find out what a C "union" is) with the datatype in one half and array of unsigned chars in the other. Here's a little demo:
Code:
#include <stdio.h>
union integeray {
int X;
unsigned char Xbytes[4];
};
int main () {
int i;
unsigned char tmp;
union integeray eg;
/* set all bytes to their highest value */
for (i=0;i<4;i++) { eg.Xbytes[i]=255; }
printf("X is %d\n",eg.X);
/* now try a different value */
for (i=0;i<4;i++) { eg.Xbytes[i]=101; }
printf("\nX is %d\n\n",eg.X);
/* now the reverse... */
eg.X = 42;
for (i=0;i<4;i++) { printf("byte %d -> %d\n",i,eg.Xbytes[i]); }
puts ("\n---------\n");
/* now swap some bytes */
tmp = eg.Xbytes[3];
eg.Xbytes[3] = eg.Xbytes[0];
eg.Xbytes[0] = tmp;
for (i=0;i<4;i++) { printf("byte %d -> %d\n",i,eg.Xbytes[i]); }
printf("X is %d\n",eg.X);
return 0;
}
Witness that because X is a signed integer, with all it's bits set in all it's bytes (the first example), X is -1.
Hopefully this can answer your question(s).