>we can not use functoins like strcpy
Then write your own strcpy function and do it that way.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
> 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?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
> 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
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953