CH 10 deitel how to program
CH 10 deitel how to program
1. Introduction
2. Structure Definitions
Example:
c
Copy code
struct employee {
char name[20];
struct employee *manager; // Self-referential
};
Example:
c
Copy code
struct card {
const char *face;
const char *suit;
} myCard, *cardPtr;
3. Initializing Structures
Example:
c
Copy code
struct card c = {"Ace", "Spades"};
Example:
c
Copy code
c.face; // Using .
ptr->face; // Using ->
6. typedef
Example:
c
Copy code
typedef struct card Card;
Card myCard;
7. Unions
8. Bitwise Operators
Example:
c
Copy code
int x = 5; // 0101 in binary
x = x << 1; // 1010 (left shift by 1)
9. Bit Fields
Specify the number of bits used for a structure member, saving memory.
Example:
c
Copy code
struct bitCard {
unsigned int face : 4; // 4 bits
unsigned int suit : 2; // 2 bits
unsigned int color : 1; // 1 bit
};
10. Enumerations
Example:
c
Copy code
enum days { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
11. Anonymous Structures and Unions
Example:
c
Copy code
struct myStruct {
int x;
struct {
int y;
int z;
};
};
Practical Questions
13. Write a struct definition for a playing card with face and suit as members.
14. Define a union that can store either an integer, a float, or a character.
15. Create a typedef alias for a structure representing a 2D point (x, y).
16. Write a C program snippet that demonstrates the use of bitwise AND and OR operators.
17. Use a bit field in a structure to represent a card with face (4 bits), suit (2 bits), and
color (1 bit).
18. Write an enum declaration for the days of the week, starting with Sunday.