Introduction
The slides cover the following topics:
C structures and unions---------------------------------------------Next slide
C structures: aggregate, yet scalar
•named members hold data items of various types
•like the notion of class/field in C or C++
– but without the data hiding features
(rationale: too inefficient)
C structures and unions---------------------------------------------Next slide
Structure declarations
struct tag {member-list} variable-list;
struct {int a, b; char *p;} x, y; /* omit tag */
int members a, b and char pointer p.
even if there is another declaration:
struct {int a, b; char *p;} z;
/* z has different type from x, y */
C structures and unions---------------------------------------------Next slide
Structure declarations
struct S {int a, b; char *p;}; /* omit variables */
struct S z; /* omit members */
typedef struct {int a, b; char *p;} S;
/* omit both tag and variables */
(more convenient than struct S)
C structures and unions---------------------------------------------Next slide
Recursively defined structures
struct TREENODE {
char *label;
struct TREENODE *leftchild, *rightchild;
}
C structures and unions---------------------------------------------Next slide
Recursively defined structures
struct HUMAN;
struct PET {
char name[NAME_LIMIT];
char species[NAME_LIMIT];
struct HUMAN *owner;
C structures and unions---------------------------------------------Next slide
Member access
C structures and unions---------------------------------------------Next slide
Memory layout
struct COST { int amount;
char currency_type[2]; }
struct PART { char id[2];
struct COST cost;
int num_avail; }
C structures and unions---------------------------------------------Next slide
Memory layout
A better alternative (from a space perspective):
struct COST { int amount;
char currency_type; }
struct PART { struct COST cost;
char id[2];
int num_avail;
C structures and unions---------------------------------------------Next slide
Bit fields
C structures and unions---------------------------------------------Next slide
Bit fields
C structures and unions---------------------------------------------Next slide
Structures as function arguments
struct BIG changestruct(struct BIG s);
– involves a lot of copying
void changestruct(struct BIG *s);
void changestruct(struct BIG const *s);
C structures and unions---------------------------------------------Next slide
Unions
union VALUE {
float f;
int i;
char *s;
};
/* either a float xor an int xor a string */
C structures and unions---------------------------------------------Next slide
Unions
C structures and unions---------------------------------------------Next slide
Unions
for the larger data types, consider using pointers instead
1. What are C structures and unions? |
2. How do we declare and define a structure in C? |
3. What is the difference between a structure and a union in C? |
4. How can we access the variables within a structure in C? |
5. What are some applications of structures and unions in C programming? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|