Structure
It is not possible to directly check equality of two structures but can compare two structures by element by element basis.
Reason: There is not a good way for a compiler to implement structure comparison (i.e. to support the == operator for structures) which is consistent with C's low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused ``holes'' in the structure (such padding is used to keep the alignment of later fields correct. A field-by-field comparison might require unacceptable amounts of repetitive code for large structures. Any compiler-generated comparison could not be expected to compare pointer fields appropriately in all cases: for example, it's often appropriate to compare char * fields with strcmp rather than ==.
Struct Hack: This technique is mainly used to create variable length array as a member in structure. We generally keep that element as last element of the structure. We can create array of zero length in the structure , we should generally keep that element as the last element of the structure. In gcc, if we create an array of zero length then that array is considered to be of incomplete type and its size is declared as of 0 bytes.
struct node
{
int no;
char name[0];
};
In the above structure , element name size is defined as of zero length so gcc will not assign memory to it. So, according to struct hack technique we can use string name as of variable length array.
Reason: There is not a good way for a compiler to implement structure comparison (i.e. to support the == operator for structures) which is consistent with C's low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused ``holes'' in the structure (such padding is used to keep the alignment of later fields correct. A field-by-field comparison might require unacceptable amounts of repetitive code for large structures. Any compiler-generated comparison could not be expected to compare pointer fields appropriately in all cases: for example, it's often appropriate to compare char * fields with strcmp rather than ==.
Struct Hack: This technique is mainly used to create variable length array as a member in structure. We generally keep that element as last element of the structure. We can create array of zero length in the structure , we should generally keep that element as the last element of the structure. In gcc, if we create an array of zero length then that array is considered to be of incomplete type and its size is declared as of 0 bytes.
struct node
{
int no;
char name[0];
};
In the above structure , element name size is defined as of zero length so gcc will not assign memory to it. So, according to struct hack technique we can use string name as of variable length array.
Comments
Post a Comment