When I compiled the follwing code snippet, it compiled without any error.
My question is if it is allowed to declare char fields inside a structure, then how can I store character string in that variable(we can not use functoins like strcpy:-| ).
OR
Is it not allowed to declare char inside?

#include <stdio.h>
#include <string.h>
int main() {
        struct aaa {
                unsigned int  a : 4;
                unsigned char  b : 24;
                unsigned int  c : 4;
                };
        struct aaa *pqr;
        }

Thanks

When I compiled the follwing code snippet, it compiled without any error.
My question is if it is allowed to declare char fields inside a structure, then how can I store character string in that variable(we can not use functoins like strcpy:-| ).
OR
Is it not allowed to declare char inside?

#include <stdio.h>
#include <string.h>
int main() {
        struct aaa {
                unsigned int  a : 4;
                unsigned char  b : 24;
                unsigned int  c : 4;
                };
        struct aaa *pqr;
        }

Thanks

Hey buddy, the above code doesnt even compile since the size of the second variable (which is of type char) exceeds its limit since it has size 8 bits and u are trying to give it 24 bits.

And if you want to store something is char why not just assign it like normal variables.

Maybe you want something like:

typedef struct aaa {
    unsigned int  a : 4;
    unsigned char  b[24];
    unsigned int  c : 4;
}myStruct ;

int main (void)
{
    myStruct obj ;
    strncpy (obj.b, "abcdefghijklmnopqrstuvwxyz", 24) ;
    printf ("%s", obj.b) ;

    return 0 ;
}

Hope it helped, bye.

> My question is if it is allowed to declare char fields inside a structure
Yes, you can have chars in struct, you can even have char arrays in structs or pointers to chars.

You can even have a char bitfield, but only with fewer bits as already noted.

> we can not use functoins like strcpy
That's because you can't point at a bit-field member.

Why are you using bitfields anyway?

Comments
Precise Wordings [Grunt]

> My question is if it is allowed to declare char fields inside a structure
Yes, you can have chars in struct, you can even have char arrays in structs or pointers to chars.

You can even have a char bitfield, but only with fewer bits as already noted.

> we can not use functoins like strcpy
That's because you can't point at a bit-field member.

Why are you using bitfields anyway?

I am using this structure for protocol header which contains three fields(4 bits, 24 bits and 4 bits respectively)as I had indicated in the structure.
Anyway thanks for the reply.

Hey buddy, the above code doesnt even compile since the size of the second variable (which is of type char) exceeds its limit since it has size 8 bits and u are trying to give it 24 bits.

And if you want to store something is char why not just assign it like normal variables.

Maybe you want something like:

typedef struct aaa {
    unsigned int  a : 4;
    unsigned char  b[24];
    unsigned int  c : 4;
}myStruct ;
 
int main (void)
{
    myStruct obj ;
    strncpy (obj.b, "abcdefghijklmnopqrstuvwxyz", 24) ;
    printf ("%s", obj.b) ;
 
    return 0 ;
}

Hope it helped, bye.

Thanks for your reply.
I am trying to pack in a single word but your structure is of 28 bytes size:-| .
As I mentioned in another reply, I am trying to use 4 bits for code, 24 bits for command(string type) and remaining 4 bits for error check(totally 32 bits == 4 bytes = word size in Linux).
Do you have any idea how this can be implemented(without exceeding 4 bytes)?

The point that if you try to keep 3 bytes (24 bits) for the string command, the last byte will be taken up by the null terminator '\0' and your command will have only two words.

Why dont you use the commands internal representation as that of hexadecimal type and convert the command typed by user in the string form to the hexadecimal type and store it in the structure.

Hope it helped, bye.

> I am using this structure for protocol header which contains three fields
Bitfields are useless for creating and manipulating external data structures.

Why?, because given

struct foo {
  unsigned int somebits:4;
}

you can't be sure whether
- the underlying storage element is 8, 16 or 32 bits (or some other number of bits in multiples of CHAR_BIT).
- whether it occupies the most or least significant bits of the word. Some machines would store it in the "xxxx0000-00000000" part of the word, and others would store it in the "00000000-0000xxxx" part of the word.
- what happens when bits cross a storage boundary
- how the total size is rounded up (padding and alignment) when you specify an non-bitfield member.


You have to assemble the bits yourself using bitwise & | << and >>.
Annoying and tedious to be sure, but there isn't an alternative which uses bitfields in any meaningful way.
http://c-faq.com/struct/bitfields.html

This article has been dead for over six months. Start a new discussion instead.