typedef struct
{
    unsigned char Val1;
    unsigned char Val2;
    unsigned char Val3;
    unsigned char ValArray1[2];
    unsigned char ValArray2[2];
    unsigned char ValArray3[2];
    unsigned char Val4;
    unsigned char Val5;
}s1;

From the above structure, would like to map only the Val3, ValArray1,ValArray2 and ValArray3 to series of bit values using ideas like structures/union. Meaning any changes to the values of Val3, ValArray1,ValArray2 and ValArray3, the bits changes on the fly.

Time to learn about bit fields. Not difficult, but we don't do your homework for you. Also, what do you mean by "changes on the fly"? What is changing the bits? An interrupt routine? Other code? Another thread?

Sorry, this is not an hemework and does not even looks like one. I am exploring things. It is just how to arrange structs and unions, in this case, that are tricky to me. It can be done from a function execution, an interrupt routine, etc.

Ditto the question regarding "changes on the fly". That could mean a lot of things. It could mean this or something else. You might need to provide an example. For example, what is supposed to happen when I do this?

s1 instance;
instance.Val3 = 7;

I just changed Val3. If you want that to trigger something else and you're NOT talking about signals, multiple threads, interrupts, etc., you can easily do all sorts of stuff in C++ that you can't do as easily in C to prevent direct access like instance.Val3 = 7. You have public versus private and member functions, setters, getters, etc. in C++ that make your task quite easy. There are weird workarounds in C that can sort of fake C++'s "private" and "member function" concepts, and you have it tagged as C, but before anyone writes an example up, is that what you want? There are also unions in C, so you can have two pointers to different types point to the same memory, thus when one "changes", the other does, but I doubt that is what you mean by "map".

yes, by the "on the fly" I meant:

typedef union
{
    unsigned char c;
    struct
    {
        unsigned char b1 : 1;
.
.
        unsigned char b8 : 1;
    }u;
}s;

if value of c changes, the b1 and b8 could change too. I liked to expand the idea to Val3, ValArray1,ValArray2 and ValArray3 in s1 struct in above. Maybe I been away from unions for long time. Time to go back to my c programing 101. Should done in C not C++.

I was thinking the OP wants an array of 8 unsigned char with allowable values of 0 or 1 for every char with allowable values of 0 to 255. So a structure and inside of that structure there is whatever there is, let's say a 32-bit unsigned int. There will be a corresponding 32 bit (or four byte) array, each containing a 0 or 1. If you use a "set" function rather than directly changing a value, you can change the corresponding bit array to the corresponding value and it's pretty easy. Something like this (expand as needed) for a simple unsigned char structure called "byte". Whatever you have in your structure, it will take 9 times the storage space because every byte of actual data has 8 bytes storing the same data, only in an 8 byte array, each element containing 0 or 1.

typedef unsigned char bits[CHAR_BIT];

typedef struct
{
    unsigned char val;
    bits theBits;
}byte;

void set(byte* aByte, unsigned char new_value)
{
    int i;
    aByte->val = new_value;
    for(i = 0; i < CHAR_BIT; i++)
    {
        aByte->theBits[i] =(new_value & 1);
        new_value >>= 1;
    }
}

as opposed to direct assignment without using the set method (in that case, bit array would not change when changing val).

Are you poiting that

typedef union
{
    unsigned char c;
    struct
    {
        unsigned char b1 : 1;
.
.
        unsigned char b8 : 1;
    }u;
}s;

takes more memory space than

typedef unsigned char bits[CHAR_BIT];
typedef struct
{
    unsigned char val;
    bits theBits;
}byte;

??.
Or, just that your code is easier to handle the set bit better?.Thank You.

I'm saying that it looks like, given what you want, that having a union isn't what you want. c and b1 have to hold the same value in your union and you want them to hold different values, so ditch the union idea.

Union would complicate it,yes.Thanks.

A union with bit-fields would provide you most of the functionality, but you can not address the bit-fields using array notation. You may want to create a function which returns the applicable bit(s) by bit-shifts and &.

c=0x65;
printf("%u\n", bitfield(c, 3)) /* display bit 3 of c. */

The union you provide could be stored in as little as 1 byte. The one with the array will need at least 9 bytes. The actual amount of memory will depend on internal alignments of the structures and unions.

If you want to work on the bits directly, there's no need for an array or a union. You have your structure, you change a data element, and the bits change with it. I don't think this is what the OP wants though. Then again the OP is exploring/experimenting and part of that is figuring out what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.