Why does the output look like this?
Posted
by
cjk
on Stack Overflow
See other posts from Stack Overflow
or by cjk
Published on 2012-10-17T22:49:05Z
Indexed on
2012/10/17
23:00 UTC
Read the original article
Hit count: 208
I have a c program below, I would like to send out a 32 bit message in a particular order Eg.0x00000001.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
struct test
{
uint16_t a;
uint16_t b;
};
int main(int argc, char const *argv[])
{
char buf[4];
struct test* ptr=(struct test*)buf;
ptr->a=0x0000;
ptr->b=0x0001;
printf("%x %x\n",buf[0],buf[1]); //output is 0 0
printf("%x %x\n",buf[2],buf[3]); //output is 1 0
return 0;
}
Then I test it by print out the values in char array. I got output in the above comments. Shouldn't the output be 0 0 and 0 1? since but[3] is the last byte? Is there anything I missed?
Thanks!
© Stack Overflow or respective owner