Structures and Unions Flashcards
what are stuctures ?
A mixed-type compound data type comprised of two or more data elements that can be of
different types
Creating New Types in C
typedef <existing> <new>;</new></existing>
Declaring Structures Types
struct <tag> {</tag>
<variable>
};
</variable>
why use tydef when making a struct?
hate typing struct <tag> whenever they need to refer to a structure’s
type. So they use a typedef to give the structure type an alternate, shorter name.</tag>
Initializing a Structure Variable
After the variable definition, the values of structure members can only be updated individually
Accessing Structure Members
cabbage . retail_price
<struct>.< member name >
</struct>
Comparing Structures
Structures cannot be directly compared for equality, even if they are of the same type.
to
determine if two structures contain exactly the same data in all of their members, the comparison has
to be done manually for each member,
if( cabbage . retail_price == fire_flakes . retail_price &&
22 cabbage . number_in_stock == fire_flakes . number_in_stock &&
23 strcmp ( cabbage . product_name , fire_flakes . product_name ) == 0 )
24 {
Greater or less than
if( strcmp ( cabbage . product_name , fire_flakes . product_name ) < 0 ) {
10.6 Unions 141
printf (“ cabbage comes before fire_flakes \n”);
}
else if( strcmp ( cabbage . product_name , fire_flakes . product_name ) > 0) {
printf (“ cabbage comes after fire_flake \n”);
}
else {
printf (“The product names are the same .\n”);