cse 324 - computer programming “using c-language”mobidev.com/shalan/lec-structures.pdf · 11...

42
1 CSE 324 - Computer Programming “Using C-Language” C Structures

Upload: others

Post on 20-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

1

CSE 324 - Computer Programming

“Using C-Language”

C Structures

Page 2: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

2

Topics

• Structures

– What are they?

– What are they good for?

• typedef

• I/O

• Structs and functions

• Array of structures

Page 3: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

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

Page 4: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

4

Structures

• For a limited number of elements

• Of varying types

• Which need to be kept together, in one place

• Can you think of examples?

Page 5: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

5

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

Page 6: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

6

Declaring Structures

struct friendStr

{

char name[MAXNAME];

long phoneNumber;

char street[MAXSTREET];

};

Every struct needs a name

Parts of the struct are known as members

This declares a type of structure,

but it does not create a variable

Page 7: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

7

Declaring structures

• To create a structure in computer memory, you need to declare a structure variable, like this:

struct friendStr sarah;

name of the type

name of the variable

Page 8: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

8

Accessing structures

• To access a member of a structure, you use the '.' operator, like this:

struct friendStr sarah;

sarah.name

sarah.phoneNumber

sarah.street

gives you access to the

value of sarah's name

Page 9: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

9

Initialising structures

struct friendStr

{

char name[MAXNAME];

long phoneNumber;

char street[MAXSTREET];

};

struct friendStr sarah;

scanf("%s",sarah.name);

scanf("%ld",&sarah.phoneNumber);

scanf("%s",sarah.street);

Page 10: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

10

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);

Page 11: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

11

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");

Page 12: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

12

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;

Page 13: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

13

typedef

• Instead, we can give the struct type a shorter name, like this:

struct friendStr

{

char name[MAXNAME];

long phoneNumber;

char street[MAXSTREET];

};

typedef struct friendStr friend;

Page 14: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

14

typedef

• Now we can use friend everywhere we used to use struct friendStrtypedef struct friendStr friend;

friend sarah;

friend tony;

friend quinn;

friend gunalwan;

friend fong;

Page 15: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

15

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."

Page 16: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

16

typedef

• The other way to use typedef is shorter, like this:typedef struct {

char name[MAXNAME];

long phoneNumber;

char street[MAXSTREET];

}friend ;

Page 17: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

17

Common Mistake

struct StudentRec{char lastname[MAXLEN];float mark;

};

Do not forget the semicolon here!

Page 18: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

18

Notes on structs • struct variables cannot be compared

• We can perform member comparisons only

if (studA == studB){printf(“Duplicate data.\n”);

}

if (strcmp(studA.lastname, studB.lastname) == 0&& (studA.mark == studB.mark) )

{printf(“Duplicate data.\n”);

}

Page 19: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

19

Notes on structs (cont)

struct StudentRec{char lastname[MAXLEN];float mark;

};struct StudentRec studA, studB, class[MAXN];

• We can define a struct, and declare instances of that struct

Page 20: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

20

typedef

• A typedef statement makes an identifier

equivalent to a type specification

struct StudentRec{char lastname[MAXLEN];float mark;

};

struct StudentRec studentA;struct StudentRec class[MAXN];

Example without typedef

Page 21: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

21

struct StudentRec{char lastname[MAXLEN];float mark;

};typedef struct StudentRec Student;

Student studA;Student class[MAXN];

Example with

typedef

typedef (cont)• The typedef statement makes an

identifier equivalent to a type specification

Page 22: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

22

Example with typedef (testing)#include <stdio.h>#define MAXLEN 50struct StudentRec{char lastname[MAXLEN];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\t%f\n", studA.lastname, studA.mark);printf("Student B: %s\t%f\n", studB.lastname, studB.mark);

return 0;}

Page 23: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

23

Example with typedef-1

#include <stdio.h>#include <stdlib.h>

#define MAXLEN 50#define MAXN 20

struct StudentRec{char lastname[MAXLEN];float mark;

};

typedef struct StudentRec Student;

int main(){int count = 0;Student class[MAXN];int i;

printf("How many students? ");scanf("%d", &count);

Page 24: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

24

if (count > MAXN){printf("Not enough space.\n");exit(1);

}for (i=0; i < count; i++){printf("Enter last name and mark: ");scanf("%s %f", class[i].lastname, &(class[i].mark) );

}

printf("\nClass list:\n\n");for (i=0; i < count; i++){printf("Last name: %s\n", class[i].lastname);printf(" Mark: %.1f\n\n", class[i].mark);

}

return 0;}

Example with typedef-2

Page 25: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

25

Function Returning a struct• A “package” containing several values

main(){Student studentA;studentA = readRecord();

}

Student readRecord ( void ){Student newStudent;printf("Enter last name and mark: ");scanf("%s %f",newStudent.lastname,&(newStudent.mark));return newStudent;

}

Page 26: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

26

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 27: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

27

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);

}

Page 28: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

Example

• int x[2] = { 5 , 10 };

• Student x = { “Ali” , 99.0 };

• Student class[2] = {

{ “ali”, 50.0 } ,

{ “Omar”, 80.0 }

};

28

Page 29: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

29

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 calledfunction.

• This time you're passing a pointer to a struct

Page 30: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

30

Passing structs by reference

void readStudent ( Student* s){printf(“Please enter name and ID\n”);scanf(“%s”, (*s).name) /*parens needed*/scanf(“%ld”, &((*s).id) ); /* Yes! */

}

int main(){Student studentA;readStudent(&studentA);

}

Page 31: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

31

Passing structs by reference

void readStudent ( Student* s){printf(“Please enter name and ID\n”);scanf(“%s”, s->name) /*parens needed*/scanf(“%ld”, &(s->id) ); /* Yes! */

}

int main(){Student studentA;readStudent(&studentA);

}

Page 32: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

32

Passing structs by reference

• The parentheses around the (*s) are needed because of precedence

• We frequently have (*sptr).item

• Better use: sptr->item

• If you need the address of an item referred to this way, you need another set of parentheses

• We frequently have &((*sptr).item)

Page 33: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

33

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 34: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

34

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 35: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

35

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 36: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

36

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 37: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

37

Arrays of structstypedef struct {

long int id;

char name[20];

} Student;

...

Student sem2Class[150];

Page 38: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

38

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 39: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

39

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 40: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

40

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 41: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

41

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 42: CSE 324 - Computer Programming “Using C-Language”mobidev.com/shalan/lec-structures.pdf · 11 Accessing structures • A member of a structure is just like any other variable •

42

Using arrays and pointers• Some struct: s[13]

• An item in some struct: s[4].age

• A character in some name: s[7].name[3]

• Address of some struct: &(s[2])

• An item in some struct pointed to: sptr->age

• Address of above: &(sptr->age)

This is an address within a struct