?
=?ISO-8859-15?Q?J=FCrgen?= Hochwald
Hi
Maybe this is a simple question, but I don't know how to solve.
Background: A weather station connected to the serial port sends data
packets. This data packets are containing variables fom one byte up to 4
byted in mixed order. Now I want to define a structure to overlay it with
UNION over the receive buffer for easily access to the single values in the
data packet.
The problem is, that I cannot defind a variable, which only reserves one
byte of memory. A simple 'char' or 'char[1]' eats 4 bytes and all following
variables are not matching.
How can I define a variable, which uses exactly one byte ?
Here is a small text app:
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c[16];
};
int main(void) {
union ovr d;
int l;
for (l=0;l<16;l++)
d.c[l]=l;
printf("size=%d\n",sizeof(d));
printf("i=%X\n",d.d.i);
printf("c=%X\n",d.d.c);
printf("j=%X\n",d.d.j);
}
--------------------------------
and the output:
size=16
i=3020100
c=4
j=B0A0908
To overlay the data packet correct, j must be 8070605.
Maybe this is a simple question, but I don't know how to solve.
Background: A weather station connected to the serial port sends data
packets. This data packets are containing variables fom one byte up to 4
byted in mixed order. Now I want to define a structure to overlay it with
UNION over the receive buffer for easily access to the single values in the
data packet.
The problem is, that I cannot defind a variable, which only reserves one
byte of memory. A simple 'char' or 'char[1]' eats 4 bytes and all following
variables are not matching.
How can I define a variable, which uses exactly one byte ?
Here is a small text app:
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
struct data {
int i;
char c;
int j;
};
union ovr {
struct data d;
char c[16];
};
int main(void) {
union ovr d;
int l;
for (l=0;l<16;l++)
d.c[l]=l;
printf("size=%d\n",sizeof(d));
printf("i=%X\n",d.d.i);
printf("c=%X\n",d.d.c);
printf("j=%X\n",d.d.j);
}
--------------------------------
and the output:
size=16
i=3020100
c=4
j=B0A0908
To overlay the data packet correct, j must be 8070605.