1 cse1301: structures 2 linda m c iver. 2 structures 2 - topics structures revision passing...

21
1 CSE1301: Structures 2 Linda M c Iver

Post on 20-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

1

CSE1301: Structures 2

Linda McIver

Page 2: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

2

Structures 2 - Topics

• Structures revision

• Passing structures as parameters

• Returning structures from functions

• arrays of structures

Page 3: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

3

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

Page 4: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

4

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

Page 5: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

5

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.

Page 6: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

6

Passing structs as parametersStudent 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);

}

Page 7: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

7

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

Page 8: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

8

Passing structs by referenceStudent fred;

readStudent(&fred);

...

void readStudent(Student* s)

{

printf("Please enter name & ID\n");

scanf("%s",s->name);

scanf("%ld",&(s->id));

}

Page 9: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

9

Passing structs by referenceStudent fred;

readStudent(&fred);

...

void readStudent(Student* s)

{

printf("Please enter name & ID\n");

scanf("%s",s->name);

scanf("%ld",&(s->id));

}

Page 10: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

10

Passing structs by referenceStudent fred;

readStudent(&fred);

...

void readStudent(Student* s)

{

printf("Please enter name & ID\n");

scanf("%s",s->name);

scanf("%ld",&(s->id));

}

Page 11: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

11

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

Page 12: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

12

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:studentList[0].name

studentList[i].id

Page 13: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

13

Array of structs

id:123456789

name: "fred"

id:123456788

name: "ralph"

id: 123456787

name: "fong"

id: 123456786

name: "rachel"

0

1

2

3

studentList

Page 14: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

14

Array of structs

id:123456789

name: "fred"

id:123456788

name: "ralph"

id: 123456787

name: "fong"

id: 123456786

name: "rachel"

0

1

2

3

studentList studentList[0]gives you the whole

struct

Page 15: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

15

Array of structs

id:123456789

name: "fred"

id:123456788

name: "ralph"

id: 123456787

name: "fong"

id: 123456786

name: "rachel"

0

1

2

3

studentList

studentList[3].name gives you the struct member

Page 16: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

16

Arrays of structstypedef struct {

long int id;

char name[20];

} Student;

...

Student sem2Class[150];

Page 17: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

17

Arrays of structsStudent sem2Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem2Class[i].name);

printf("enter id\n");

scanf("%d",&(sem2Class[i].id));

}

name of array

Page 18: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

18

Arrays of structsStudent sem2Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem2Class[i].name);

printf("enter id\n");

scanf("%d",&(sem2Class[i].id));

}

index into array

Page 19: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

19

Arrays of structsStudent sem2Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem2Class[i].name);

printf("enter id\n");

scanf("%d",&(sem2Class[i].id));

}

the structure at this position in

the array

Page 20: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

20

Arrays of structsStudent sem2Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem2Class[i].name);

printf("enter id\n");

scanf("%d",&(sem2Class[i].id));

}

name of the member of the structure at that position in the

array

Page 21: 1 CSE1301: Structures 2 Linda M c Iver. 2 Structures 2 - Topics Structures revision Passing structures as parameters Returning structures from functions

21

Reading

• Deitel and Deitel- Sections 10.1 to 10.7