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 ID: 757985
Download Presentation The PPT/PDF document "structs Adapted from Dr. Mary Eberlein, ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
structs
Adapted from Dr. Mary Eberlein, UT AustinSlide2
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
structSlide3
Example
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");
structure tagSlide4
structs
Record containing related data values that are used together
Fields
stored in contiguous memory
Like an array, except:
data values in struct have namesaccess 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 structSlide5
Structures
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) {…}Slide6
Field AccessUse 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); Slide7
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 0Slide8
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 part2Note: structs cannot be compared with
== or != Slide9
Passing structs
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: ElvisLast name: PresleySlide10
struct return values
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"); Slide11
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: BruceLast name: LeeFirstname: BruceLast name: Lee