Anup Joshi wrote:
> hello Vic,thank you for your comments
>
> now i understand that the last element of an array could not be always used
no, you misunderstand what uchar y[1] means. I hear there is a lanugage
out there (I don't know which, and I don't WANT to know) in which that
defines an array in which y[1] is a valid element. This is NOT true in
C or C++. It means there is an array with 1 element which is referenced
as y[0]. The number between the brackets in a definition is the NUMBER
of elements (e.g. uchar y[n]; has n elements, numbered from 0 through
n-1 inclusive).
> but considering our friend's (brian's) problem could we create an array of
> integer of size 3 i.e. uchar y[2];
as explained above uchar y[2]; does NOT have 3 elements.
> then i hope it will work on all the systems. could you all give ur comments
> on that
>
> with regards
>
you still have the "endian" problem...
the proper way to deal with this is for the OP to write (in C)
int x = (int)msb * 256 + lsb;
THAT should work on all systems.
> Anup
> >
> > unions.c:
> > ============ ========= ========= ========= =
> >
> > #include <stdio.h>
> >
> > union vals {
> > unsigned short x;
> > unsigned char y[1];
> > } myvals;
> >
> > int main() {
> > myvals.y[0] = 1; //lsb -- 00000001
> > myvals.y[1] = 21; //msb --00010101
> >
>
> unsigned char y[1] only has a valid subscript of 0
> whether your solution works will depend on whether you're on a big or
> little endian system
>
> > printf("%d", myvals.x); // displaying 16 bit uint 0001010100000001
> > printf("\n %d", sizeof(myvals) );
> > }
> >
> > ============ ========= ========= ========= ===
> > and its output is :
> >
> > 5377
> > 2
> >
>