+ All Categories
Home > Documents > Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer...

Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer...

Date post: 30-Mar-2021
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
42
1 Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering [email protected]
Transcript
Page 1: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

1

Introduction to Computer

Programming

Lecture 16

Structures (Part 1)

Assist. Prof. Dr. Nükhet ÖZBEK

Ege University

Department of Electrical & Electronics Engineering

[email protected]

Page 2: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

2

Topics

• Structure

• Arrays of structs

• typedef

• structs and functions

• Pointers to structs

• structs within structs

• Data structures and modular design

Page 3: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

3

What is a Structure?

• In C, we can define our own data types that

represent structured collections of data

• A collection of related variables under one

aggregate name

• May contain variables of different types

• Using structures:

– Define the structure

– Declare/Initialize instances of the structure

– Access members of an instance of the structure

Page 4: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

4

Structure Definition

Lunchbox

Define a structure called

“Lunchbox” which has the

following compartments:

• a fruit compartment

• a sandwich compartment

• a drink compartment

• A structure is a blueprint

• Example:

Page 5: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

5

Structure Definition (cont)

• A struct is used to specify the blueprint

• The members specify different aspects of a struct

• Example:

struct LunchBox

{

int fruit;

float drink;

char sandwich[MAXN];

}; Lunchbox

Page 6: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

6

Structure Variable

• Instance of a structure: actual series of

contiguous memory locations for storage

struct LunchBox ali;

struct LunchBox veli,ayse,fatma;

struct LunchBox kids[5];

Page 7: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

7

Initializing a struct Variable

struct LunchBox ali = {1,370.0,“kofte”};

Page 8: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

8

Initializing a struct Variable (cont)

struct LunchBox kids[5] =

{

{1, 370.0, “kofte” },

{2, 100.0, “doner”},

{0, 0.0, “kebap”},

{1, 300.0, “salata”},

{0, 0.0, “”}

};

0 1 2 3 4

Page 9: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

9

Members of a struct Variable

struct LunchBox ali, veli;

veli.drink = 0.0;

veli.fruit = 0;

strcpy(veli.sandwich,“doner”);

ali.fruit = 1;

strcpy(ali.sandwich, “kofte”);

ali.drink = 300.0;

Page 10: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

10

Members of a struct Variable

struct LunchBox kids[3];

int index = 1;

kids[0].drink = 300.0;

kids[0].fruit = 1;

strcpy(kids[2].sandwich,“kofte”);

kids[index].fruit = 3;

0

1

2

kids[index+1].drink =

kids[index - 1].drink;

Page 11: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

11

Input/Output of struct

• Library functions printf() and scanf() do not

have format conversion specifiers for structs

• Input/Output for each member only

struct LunchBox ayse;

scanf(“%d”, &(ayse.fruit));

scanf(“%f”, &(ayse.drink));

scanf(“%s”, ayse.sandwich);

printf(“%d, %f\n”,

ayse.fruit, ayse.drink);

printf(“%s\n”, ayse.sandwich);

Page 12: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

12

Input/Output of struct (cont) struct LunchBox kids[3];

int i = 0;

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

{

scanf(“%d %f %s”,

&(kids[i].fruit),&(kids[i].drink),

kids[i].sandwich);

}

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

{

printf(“%d, %f, %s\n”,

kids[i].fruit, kids[i].drink,

kids[i].sandwich);

}

0

1

2

Page 13: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

13

Example: Student Record

• Write a program to read in and print a list of

student last names and test marks

input number of students

for each student in the list

{

input last name and mark

}

for each student in the list

{

output last name and mark

}

Page 14: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

14

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

int main()

{

char lastname[MAXN][MAXLEN];

float mark[MAXN];

int count = 0;

int i;

printf("How many students? ");

scanf("%d", &count);

marks1.c

Example without struct-1

Page 15: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

15

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", lastname[i], &mark[i]);

}

printf("\nClass list:\n\n");

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

{

printf("Last name: %s\n", lastname[i]);

printf(" Mark: %.1f\n\n", mark[i]);

}

return 0;

} marks1.c

Example without struct-2

Page 16: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

16

Example: Student Record

• Define a struct: StudentRec

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

• Easy to extend later to include ID number,

first name, etc

Page 17: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

17

Example with struct (testing)

#include <stdio.h>

#define MAXLEN 50

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

int main()

{

struct StudentRec studA;

struct StudentRec 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;

} marks2a.c

Page 18: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

18

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

int main()

{

struct StudentRec class[MAXN];

int count = 0;

int i;

printf("How many students? ");

scanf("%d", &count); marks3a.c

Example with struct-1

Page 19: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

19

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;

} marks3a.c

Example with struct-2

Page 20: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

20

Common Mistake

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

Do not forget the

semicolon here!

Page 21: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

21

Notes on structs

• Initialization vs. Assignment

struct StudentRec studA = {“Ali”, 90};

struct StudentRec studA;

studA = {“Ali”, 90};

struct StudentRec studA = {“Ali”, 90};

struct StudentRec studB;

studB = studA; /* struct contains pointers? */

Page 22: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

22

Notes on structs (cont)

• 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 23: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

23

Notes on structs (cont)

struct StudentRec

{

char lastname[MAXLEN];

float mark;

} studA, studB, class[MAXN];

• We can define a struct, and declare instances

of that struct

Page 24: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

24

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 25: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

25

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 26: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

26

Example with typedef (testing) #include <stdio.h>

#define MAXLEN 50

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

} marks2b.c

Page 27: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

27

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); marks3b.c

Page 28: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

28

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;

}

marks3b.c

Example with typedef-2

Page 29: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

29

Notes on typedef

• Yet another way of using typedef:

typedef struct

{

char lastname[MAXLEN];

float mark;

} Student;

Student studA, studB;

Student class[MAXN];

Page 30: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

30

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 31: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

31

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;

} Version 1

Page 32: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

32

Function Returning a struct (cont)

Student readRecord ( Student newStudent )

{

printf("Enter last name and mark: ");

scanf("%s %f",newStudent.lastname,&(newStudent.mark));

return newStudent;

}

main()

{

Student studentA;

studentA = readRecord(studentA);

}

Version 2

• A “package” containing several values

Page 33: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

33

Example: Structs and Functions-1 #include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec

{

char lastname[MAXLEN];

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

Page 34: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

34

int main()

{

int count = 0;

Student class[MAXN];

int i;

printf("How many students? ");

scanf("%d", &count);

if (count > MAXN)

{

printf("Not enough space.\n");

exit(1);

}

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

{

class[i] = readRecord();

}

printf("\nClass list:\n\n");

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

{

printRecord(class[i]);

}

return 0;

} marks4a.c

Example: Structs and Functions-2

Page 35: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

35

Passing a struct Pointer

Pass a pointer to

the struct variable

instead!

lastname:

mark:

studentA:

studentPtr:

Page 36: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

36

void readRecord ( Student *studentPtr )

{

/* De-reference pointer here. */

}

int main()

{

Student studentA;

readRecord( &(studentA) );

return 0;

}

Passing a struct Pointer

lastname:

mark:

studentA:

studentPtr:

lastname:

Page 37: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

37

To de-reference a pointer to a

struct variable:

Style 1: Use the * operator

void readRecord ( Student *studentPtr )

{

printf("Enter last name and mark: ");

scanf("%s %f", (*studentPtr).lastname,

&((*studentPtr).mark) );

}

marks4b.c

Passing a struct Pointer

studentPtr:

mark:

studentA:

lastname:

Page 38: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

38

To de-reference a pointer to a

struct variable:

Style 2: Use the -> operator

void readRecord ( Student *studentPtr )

{

printf("Enter last name and mark: ");

scanf("%s %f", studentPtr->lastname,

&(studentPtr->mark));

}

marks4b.c

Passing a struct Pointer

studentPtr:

mark:

studentA:

lastname:

Page 39: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

39

Example: Pointers to Structs-1 #include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

typedef struct StudentRec Student;

void readRecord ( Student *studentPtr )

{

printf("Enter last name and mark: ");

scanf("%s %f", studentPtr->lastname, &(studentPtr->mark));

}

void printRecord ( Student *item )

{

printf("Last name: %s\n", (*item).lastname);

printf(" Mark: %.1f\n\n", (*item).mark);

} marks4c.c

Page 40: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

40

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

typedef struct StudentRec Student;

void readRecord ( Student *studentPtr )

{

printf("Enter last name and mark: ");

scanf("%s %f", studentPtr->lastname, &(studentPtr->mark) );

}

void printRecord ( Student *item )

{

printf("Last name: %s\n", (*item).lastname);

printf(" Mark: %.1f\n\n", (*item).mark);

} marks4c.c

Example: Pointers to Structs-1 (cont)

Page 41: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

41

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec

{

char lastname[MAXLEN];

float mark;

};

typedef struct StudentRec Student;

void readRecord ( Student *studentPtr )

{

printf("Enter last name and mark: ");

scanf("%s %f", studentPtr->lastname, &(studentPtr->mark) );

}

void printRecord ( Student *item )

{

printf("Last name: %s\n", (*item).lastname);

printf(" Mark: %.1f\n\n", (*item).mark);

} marks4c.c

Example: Pointers to Structs-1 (cont)

Page 42: Introduction to Computer Programming Lecture 16 Structures ... · Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University

42

int main()

{

int count = 0;

Student class[MAXN];

int i;

printf("How many students? ");

scanf("%d", &count);

if (count > MAXN)

{

printf("Not enough space.\n");

exit(1);

}

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

{

readRecord( &(class[i]) );

}

printf("\nClass list:\n\n");

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

{

printRecord( &(class[i]) );

}

return 0;

} marks4c.c

Example: Pointers to Structs-2


Recommended