structs, unions, and enums - dalhousie universitynzeh/teaching/2132/slides/structs.pdf ·...
TRANSCRIPT
![Page 1: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/1.jpg)
CSCI 2132: Software Development
Structs, Unions,and Enums
Norbert Zeh
Faculty of Computer Science Dalhousie University
Winter 2019
![Page 2: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/2.jpg)
Structs
Combine constant number of values of possibly different types into a record
Contrast with arrays:
• Arbitrary number of values
• All the same type
Important uses:
• Representing records in data collections
• Building block for data structures
struct student { unsigned int number; char name[32]; char username[9]; };
Elements are called members
Members referred to by name
![Page 3: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/3.jpg)
Dot OperatorStruct members are accessed using dot notation, similar to Java:
struct student { unsigned int number; char name[32]; char username[9]; };
int main() { struct student s; s.number = 321459; strcpy(s.name, “A Student”); strcpy(s.username, “astudent”); printf(“Student number: %8d\n”, s.number); printf(“Name: %s\n”, s.name); printf(“Username: %s\n”, s.username); return 0; }
Need to write struct here. (A tad annoying.)
![Page 4: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/4.jpg)
Arrow OperatorArrow operator (->) combines pointer dereferencing and member access:
struct student { unsigned int number; char name[32]; char username[9]; };
void print_name(struct student *s) { printf(“%s\n”, (*s).name); }
int main() { struct student s; ... print_name(&s); }
![Page 5: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/5.jpg)
Arrow OperatorArrow operator (->) combines pointer dereferencing and member access:
struct student { unsigned int number; char name[32]; char username[9]; };
void print_name(struct student *s) { printf(“%s\n”, s->name); }
int main() { struct student s; ... print_name(&s); }
![Page 6: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/6.jpg)
struct student { unsigned int number; char name[32]; char username[9]; };
void print_name(struct student *s) { printf(“%s\n”, s->name); }
int main() { struct student s; ... print_name(&s); }
A Common Idiom to Avoid Typing
![Page 7: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/7.jpg)
typedef struct { unsigned int number; char name[32]; char username[9]; } student;
void print_name(student *s) { printf(“%s\n”, s->name); }
int main() { student s; ... print_name(&s); }
A Common Idiom to Avoid Typing
![Page 8: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/8.jpg)
UnionsStruct stores all its members at the same time. Union stores only one of its members at a time. Size = maximum size of its members.
struct foo { int x; double y; };
union bar { int x; double y; };
yx
x
y
or
![Page 9: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/9.jpg)
Uses of UnionsSaving space:
struct student { int banner; char name[32]; char faculty[32]; char major[32]; struct ac_record *record; }
struct employee { int banner; char name[32]; char faculty[32]; int salary; struct emp_hist *history; }struct banner_record {
int banner; char name[32]; char faculty[32]; char major[32]; int salary; struct ac_record *record; struct emp_hist *history; }
![Page 10: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/10.jpg)
Uses of UnionsSaving space:
struct student_details { char major[32]; struct ac_record *record; };
struct employee_details { int salary; struct emp_hist *history; };
![Page 11: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/11.jpg)
Uses of UnionsSaving space:
struct student_details { char major[32]; struct ac_record *record; };
struct employee_details { int salary; struct emp_hist *history; };
union details { struct student_details; struct employee_details; };
![Page 12: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/12.jpg)
Uses of UnionsSaving space:
struct student_details { char major[32]; struct ac_record *record; };
struct employee_details { int salary; struct emp_hist *history; };
union details { struct student_details; struct employee_details; };
struct student { int banner; char name[32]; char faculty[32]; int type; union details dtls; }
Need a way to figure out the record type.
![Page 13: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/13.jpg)
Uses of Unions
Re-interpreting bit content:
union converter { double d; unsigned long int bits; };
int main() { union converter c; c.d = 5.312e2; printf(“%lx\n”, c.bits); return 0; }
![Page 14: Structs, Unions, and Enums - Dalhousie Universitynzeh/Teaching/2132/Slides/structs.pdf · 2019-11-04 · and Enums Norbert Zeh Faculty of Computer Science Dalhousie University Winter](https://reader034.vdocuments.mx/reader034/viewer/2022042911/5f448fe8549cff39562da2c8/html5/thumbnails/14.jpg)
EnumsAre very similar to their use in Java:
enum color { RED, GREEN, BLUE };
int main() { enum color col = BLUE; printf(“%d\n”, col); return 0; }
enum flags { READABLE = 0x04, WRITABLE = 0x02, EXECUTABLE = 0x01 };
int main() { enum flags fs = READABLE | WRITABLE; printf(“%d\n”, fs); return 0; }
They are just integers in disguise in C.