Structures - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Structures

Description:

struct friend sarah; name of the type. name of the variable. 9 ... gives you access to the value of sarah's name. 10. Initialising structures. struct friendStr ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 49
Provided by: joeychuaan
Category:
Tags: sarah | structures

less

Transcript and Presenter's Notes

Title: Structures


1
Structures
2
Topics
  • Structures
  • What are they?
  • What are they good for?
  • typedef
  • Arrays of structs
  • structs and functions

3
Structures
  • Arrays hold many elements of the same type
  • What happens if we want to keep a few things
    together that are of different types?
  • For example, the title, artist and price of the
    CDs in a shop
  • Or name and telephone number
  • Or name, ID number, and mark

4
Structures
  • For a limited number of elements
  • Of varying types
  • Which need to be kept together, in one place
  • We can use a structure
  • Like a box, with compartments for different things

5
Structures
  • Like my briefcase, which has compartments for
    different shaped things
  • Or a cutlery draw which has different sections
    for teaspoons, knives, forks and spoons
  • Can you think of more examples?

6
Structures
  • In C, a structure is known as a struct
  • It contains a fixed number of parts, which may be
    of different types
  • So for a friend, you may want to store name,
    phone number and the street they live in

7
Declaring Structures
Every struct needs a name
  • struct friend
  • char nameMAXNAME
  • long phoneNumber
  • char streetMAXSTREET

Parts of the struct are known as members
This declares a type of structure, but it does
not create a variable
8
Declaring structures
  • To create a structure, you need to declare a
    structure variable, like this
  • struct friend sarah

name of the type
name of the variable
9
Accessing structures
  • To access a member of a structure, you use the
    '.' operator, like this
  • struct friend sarah
  • sarah.name
  • sarah.phoneNumber
  • sarah.street

gives you access to the value of sarah's name
10
Initialising structures
  • struct friendStr
  • char nameMAXNAME
  • long phoneNumber
  • char streetMAXSTREET
  • struct friendStr sarah
  • scanf("s",sarah.name)
  • scanf("ld",sarah.phoneNumber)
  • scanf("s",sarah.street)

11
Accessing structures
  • struct friendStr sarah
  • scanf("s",sarah.name)
  • scanf("ld",sarah.phoneNumber)
  • scanf("s",sarah.street)
  • printf("Name is s\n",sarah.name)
  • printf("Phone is d\n",sarah.phoneNumber)
  • printf("Street is s\n",sarah.street)

12
Accessing structures
  • A member of a structure is just like any other
    variable
  • If it's a string, it's just an ordinary string
  • If it's an int, it's just an ordinary int
  • EXCEPT that you access them using the name of the
    struct variable, AND the name of the member
  • sarah.phoneNumber 55559999
  • strcpy(sarah.name,"Sarah Finch")
  • strcpy(sarah.street,"Firthsmith St")

13
Accessing structures
  • If you want to declare a lot of structs, using
    "struct name" all the time is awkward
  • struct friendStr sarah
  • struct friendStr tony
  • struct friendStr quinn
  • struct friendStr gunalwan
  • struct friendStr fong

14
typedef
  • Instead, we can give the struct type a shorter
    name, like this
  • struct friendStr
  • char nameMAXNAME
  • long phoneNumber
  • char streetMAXSTREET
  • typedef struct friendStr friend

15
typedef
  • Now we can use friend everywhere we used to use
    struct friendStr
  • typedef struct friendStr friend
  • friend sarah
  • friend tony
  • friend quinn
  • friend gunalwan
  • friend fong

16
typedef
  • All we have done is told the compiler
  • "every time you see friend, I really mean struct
    friendStr."
  • In the same way we use symbolic constant
    declarations like "define SIZE 20" to tell the
    compiler
  • "every time you see SIZE, I really mean 20."

17
typedef
  • The other way to use typedef is shorter, like
    this
  • typedef struct
  • char nameMAXNAME
  • long phoneNumber
  • char streetMAXSTREET
  • friend

18
Common Mistake
struct StudentRec char lastnameMAXLEN
float mark
Do not forget the semicolon here!
19
Notes on structs
  • struct variables cannot be compared
  • We can perform member comparisons only

20
Notes on structs (cont)
  • We can define a struct, and declare instances of
    that struct

struct StudentRec char lastnameMAXLEN
float mark struct StudentRec studA, studB,
classMAXN
21
typedef
  • A typedef statement makes an identifier
    equivalent to a type specification

struct StudentRec char lastnameMAXLEN
float mark
Example without typedef
struct StudentRec studentA struct StudentRec
classMAXN
22
typedef (cont)
  • The typedef statement makes an identifier
    equivalent to a type specification

struct StudentRec char lastnameMAXLEN
float mark typedef struct StudentRec Student
Example with typedef
Student studA Student classMAXN
23
Example with typedef (testing)
include define MAXLEN 50 struct
StudentRec char lastnameMAXLEN float
mark typedef struct StudentRec Student int
main() Student studA Student studB
printf("Enter last name and mark for student A
") scanf("s f", studA.lastname,
(studA.mark)) printf("Enter last name and
mark for student B ") scanf("s f",
studB.lastname, (studB.mark))
printf("Student A s\tf\n", studA.lastname,
studA.mark) printf("Student B s\tf\n",
studB.lastname, studB.mark) return 0
marks2b.c
24
Example with typedef-1
include include define
MAXLEN 50 define MAXN 20 struct
StudentRec char lastnameMAXLEN float
mark typedef struct StudentRec Student int
main() int count 0 Student
classMAXN int i printf("How many
students? ") scanf("d", count)
marks3b.c
25
Example with typedef-2
if (count MAXN) printf("Not enough
space.\n") exit(1) for (i0 i count i) printf("Enter last name and
mark ") scanf("s f", classi.lastname,
(classi.mark) ) printf("\nClass
list\n\n") for (i0 i printf("Last name s\n", classi.lastname)
printf(" Mark .1f\n\n", classi.mark)
return 0
marks3b.c
26
Passing a struct to a Function
  • As always, the formal parameters are copies of
    the actual parameters

void printRecord ( Student item )
printf("Last name s\n", item.lastname)
printf(" Mark .1f\n\n", item.mark)
main() Student studentA Gauss, 99.0
printRecord(studentA)
27
Function Returning a struct
  • A package containing several values

Student readRecord ( void ) Student
newStudent printf("Enter last name and mark
") scanf("s f",newStudent.lastname,(newStude
nt.mark)) return newStudent
main() Student studentA studentA
readRecord()
28
Example Structs and Functions-1
include include define
MAXLEN 50 define MAXN 20 struct
StudentRec char lastnameMAXLEN float
mark typedef struct StudentRec
Student Student readRecord ( void ) Student
newStudent printf("Enter last name and mark
") scanf("s f", newStudent.lastname,
(newStudent.mark)) return
newStudent void printRecord ( Student item
) printf("Last name s\n", item.lastname)
printf(" Mark .1f\n\n", item.mark)
marks4a.c
29
Example Structs and Functions-2
int main() int count 0 Student
classMAXN int i printf("How many
students? ") scanf("d", count) if
(count MAXN) printf("Not enough
space.\n") exit(1) for (i0 i count i) classi readRecord()
printf("\nClass list\n\n") for (i0 i count i) printRecord(classi)
return 0
marks4a.c
30
Remaining Topics
  • Structures revision
  • Passing structures as parameters
  • Returning structures from functions
  • arrays of structures

31
Structs - revision
  • collection of members of different types
  • good for storing related values
  • to refer to a member of a struct you need the
    struct name and the member name, joined by a '.'
  • eg. studentA.name, studentA.id

32
Structs - revision (cont)
  • structs can contain members of any type (basic
    types, arrays, other structs, pointers, etc)
  • A struct declaration declares a type
  • to use it, you need to declare a variable of that
    type
  • Can use typedef to rename the struct type
  • Can't compare structs directly, can only compare
    members

33
Passing structs as parameters
  • Like any other variable, you can pass a struct as
    a parameter to a function
  • First we'll look at passing by value
  • Pass the struct in, use the values of the
    members, but don't change them.

34
Passing structs as parameters
Student fred scanf("s",fred.name) scanf("ld
",fred.id) printStudent(fred) ... void
printStudent(Student s) printf("Student's
name is s\n",s.name) printf("Student's id is
ld\n",s.id)
35
Passing structs as parameters
  • You can also pass structs by reference
  • Pass the struct in, change the value of some or
    all of the members, changes are visible in the
    calling function as well as the called function.
  • This time you're passing a pointer to a struct

36
Passing structs by reference
Student fred readStudent(fred) ... void
readStudent(Student s) printf("Please enter
name ID\n") scanf("s",s-name)
scanf("ld",(s-id))
37
Passing structs by reference
Student fred readStudent(fred) ... void
readStudent(Student s) printf("Please enter
name ID\n") scanf("s",s-name)
scanf("ld",(s-id))
38
Passing structs by reference
Student fred readStudent(fred) ... void
readStudent(Student s) printf("Please enter
name ID\n") scanf("s",s-name)
scanf("ld",(s-id))
39
Passing structs by reference
  • So when you pass a struct as a pointer, the
    operator you use to access a member changes from
    '.' to '-'
  • It's an arrow, because your parameter points to
    the struct
  • Note that you still need the ampersand to get the
    address of a member of the struct

40
Arrays of structs
  • You can have an array of structs
  • Each element of the array is a whole struct, with
    all the members of that struct.
  • So to access a single value, you need to know
    which element of the array you're dealing with,
    and which member of the struct
  • studentList0.name
  • studentListi.id

41
Array of structs
studentList
42
Array of structs
studentList0gives you the whole struct
studentList
43
Array of structs
studentList
studentList3.name gives you the struct member
44
Arrays of structs
typedef struct long int id char name20
Student ... Student sem2Class150
45
Arrays of structs
Student sem2ClassMAXCLASS int i for
(i0iname\n") scanf("s",sem2Classi.name) printf(
"enter id\n") scanf("d",(sem2Classi.id))
name of array
46
Arrays of structs
Student sem2ClassMAXCLASS int i for
(i0iname\n") scanf("s",sem2Classi.name) printf(
"enter id\n") scanf("d",(sem2Classi.id))
index into array
47
Arrays of structs
Student sem2ClassMAXCLASS int i for
(i0iname\n") scanf("s",sem2Classi.name) printf(
"enter id\n") scanf("d",(sem2Classi.id))
the structure at this position in the array
48
Arrays of structs
Student sem2ClassMAXCLASS int i for
(i0iname\n") scanf("s",sem2Classi.name) printf(
"enter id\n") scanf("d",(sem2Classi.id))
name of the member of the structure at that
position in the array
Write a Comment
User Comments (0)
About PowerShow.com