Download presentation
Presentation is loading. Please wait.
Published byAune Seppälä Modified over 5 years ago
1
structs Adapted from Dr. Mary Eberlein, UT Austin
2
Structures struct: Collection of variables with one name
the variables are called members or fields may be different types Use structs to keep related data together pass fewer arguments return multiple values as a struct
3
Example A struct for data related to our class: Or use a typedef:
structure tag A struct for data related to our class: struct eeClass { int classNum; char meetingRoom[20]; char courseName[30]; }; Variable declaration: struct eeClass ee312; Member (or field) access: ee312.classNum = 312; Or use a typedef: typedef struct eeClass{ int classNum; char meetingRoom[20]; char courseName[30]; } EEClass; EEClass ee312; ee312.classNum = 312; strcpy(ee312.meetingRoom, "EER3");
4
structs Record containing related data values that are used together
Fields stored in contiguous memory Like an array, except: data values in struct have names access fields with "dot" operator, not [] Suppose you are writing a class scheduling system. You'd probably need to pass the same set of data to many functions. void catalog(char courseName[], int courseNumber, int secID) {…} Instead: combine that data in a struct Short for "structured data type" Bundle set of data into a single thing called a struct.
5
Structures Now your function might look like this:
struct UTCourse { char courseName[50]; int courseNumber; int secID; }; struct UTCourse EE312_00 = {"Software Design & Implementation I", 312, 16100}; Now your function might look like this: void catalog(struct UTCourse myClass) {…}
6
Field Access Use the dot operator to access fields (and the variable name) typedef struct FullName { char first[20]; char last[20]; } FullName; FullName me = {“Roger", “Priebe"}; printf("Hey %s\n", me.last);
7
Designated Initializers (C99)
typedef struct name { char first[20]; char last[20]; } FullName; Value can be labeled with the field name: FullName person = {.last = "Presley", .first = "Elvis"}; If field omitted from initializer, set to 0
8
Operations on structs The . access operator has precedence over nearly all other operators Example: typedef struct { int partNum; char partName[30]; int onHand; } Part; Part part1 = {.partNum = 311, .partName = "usb"}; scanf("%d", &part1.onHand); // . operator higher precedence than & Assigning one struct to another makes a copy: part2 = part1; // copy each field of part1 into part2 Note: structs cannot be compared with == or !=
9
Passing structs Function call: printName(person); Output:
void printName(struct Name p) { printf("First name: %s\n", p.first); printf("Last name: %s\n", p.last); } Function call: printName(person); Output: First name: Elvis Last name: Presley Passing a structure to a function or returning a struct both require making a copy of all fields in the structure.
10
struct return values Function call:
struct Name makeAName(char *firstName, char* lastName) { struct name elvis; strcpy(elvis.first, firstName); strcpy(elvis.last, lastName); return elvis; } Function call: struct Name theKing = makeAName("Elvis", "Presley");
11
Example #include<stdio.h> #include<string.h> typedef struct { char first[20]; char last[20]; } FullName; void printName(FullName p); FullName makeAName(char *firstName, char *lastName); int main() { FullName onePerson = makeAName("Bruce", "Lee"); printName(onePerson); FullName someone = onePerson; printName(someone); } Output: First name: Bruce Last name: Lee Firstname: Bruce
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.