Introduction to Computer Programming Lecture 16 Structures (Part 1)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Structures Spring 2013Programming and Data Structure1.
Structures in C.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 20 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 21 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 2)
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Lecture 13 Static vs Dynamic Memory Allocation
Edited from Powerpoint Slides provided by Thomson Learning
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Data Structures and Algorithms Programs. Different problems. Operations. Size of data. Resources available.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Structures, Unions, Enumerations
Computer Science 210 Computer Organization
CS1010 Programming Methodology
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming Tutorial – Part I
Introduction to Computer Programming Lecture 18 Binary Files
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Module 2 Arrays and strings – example programs.
CSI-121 Structured Programming Language Lecture 16 Pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Programming Lecture 15 Text File I/O
Computer Science 210 Computer Organization
CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)
Programming in C Pointer Basics.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Introduction to Computer Programming Lecture 17 Structures (Part 2)
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
Chapter 9: Records (structs)
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
Programming in C Pointer Basics.
Functions, Part 2 of 3 Topics Functions That Return a Value
Programming in C Pointer Basics.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Introduction to Computing Lecture 10 Arrays (Part 1)
Introduction to Computer Organization & Systems
Introduction to Computing Lecture 04: Booleans & Selection
Introduction to Computing Lecture 08: Functions (Part I)
Programming Languages and Paradigms
Programming in C Pointer Basics.
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.2160 ECE Application Programming
Chapter 9: Records (structs)
Functions, Part 2 of 3 Topics Functions That Return a Value
Structures, Unions, and Enumerations
Presentation transcript:

Introduction to Computer Programming Lecture 16 Structures (Part 1) ITCS 102 Computer Programming Introduction to Computer Programming Lecture 16 Structures (Part 1) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Dr. B. KARLIK

Topics Structure Arrays of structs typedef structs and functions Pointers to structs structs within structs Data structures and modular design

ITCS 102 Computer Programming 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 Dr. B. KARLIK

ITCS 102 Computer Programming Structure Definition A structure is a blueprint Example: Lunchbox Define a structure called “Lunchbox” which has the following compartments: a fruit compartment a sandwich compartment a drink compartment Dr. B. KARLIK

Structure Definition (cont) ITCS 102 Computer Programming 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 Dr. B. KARLIK

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

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

Initializing a struct Variable (cont) 1 2 3 4 struct LunchBox kids[5] = { {1, 370.0, “kofte” }, {2, 100.0, “doner”}, {0, 0.0, “kebap”}, {1, 300.0, “salata”}, {0, 0.0, “”} };

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;

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; 1 kids[index+1].drink = kids[index - 1].drink; 2

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

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); } printf(“%d, %f, %s\n”, kids[i].fruit, kids[i].drink, 1 2

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 } output last name and mark

Example without struct-1 ITCS 102 Computer Programming Example without struct-1 #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 Dr. B. KARLIK

Example without struct-2 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"); printf("Last name: %s\n", lastname[i]); printf(" Mark: %.1f\n\n", mark[i]); return 0; marks1.c

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

Example with struct (testing) ITCS 102 Computer Programming 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 Dr. B. KARLIK

ITCS 102 Computer Programming Example with struct-1 #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 Dr. B. KARLIK

Example with struct-2 { printf("Not enough space.\n"); exit(1); } 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"); printf("Last name: %s\n", class[i].lastname); printf(" Mark: %.1f\n\n", class[i].mark); return 0; marks3a.c

Do not forget the semicolon here! Common Mistake struct StudentRec { char lastname[MAXLEN]; float mark; }; Do not forget the semicolon here!

ITCS 102 Computer Programming 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? */ Dr. B. KARLIK

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

Notes on structs (cont) We can define a struct, and declare instances of that struct struct StudentRec { char lastname[MAXLEN]; float mark; } studA, studB, class[MAXN];

ITCS 102 Computer Programming typedef A typedef statement makes an identifier equivalent to a type specification struct StudentRec { char lastname[MAXLEN]; float mark; }; Example without typedef struct StudentRec studentA; struct StudentRec class[MAXN]; Dr. B. KARLIK

typedef (cont) The typedef statement makes an identifier equivalent to a type specification struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; Example with typedef Student studA; Student class[MAXN];

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

Example with typedef-1 marks3b.c #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

Example with typedef-2 marks3b.c { printf("Not enough space.\n"); 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"); printf("Last name: %s\n", class[i].lastname); printf(" Mark: %.1f\n\n", class[i].mark); return 0; marks3b.c

ITCS 102 Computer Programming Notes on typedef Yet another way of using typedef: typedef struct { char lastname[MAXLEN]; float mark; } Student; Student studA, studB; Student class[MAXN]; Dr. B. KARLIK

Passing a struct to a Function ITCS 102 Computer Programming 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); } Dr. B. KARLIK

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,&(newStudent.mark)); return newStudent; } Version 1 main() { Student studentA; studentA = readRecord(); }

Function Returning a struct (cont) A “package” containing several values Student readRecord ( Student newStudent ) { printf("Enter last name and mark: "); scanf("%s %f",newStudent.lastname,&(newStudent.mark)); return newStudent; } Version 2 main() { Student studentA; studentA = readRecord(studentA); }

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

Example: Structs and Functions-2 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"); printRecord(class[i]); return 0; marks4a.c

Passing a struct Pointer Pass a pointer to the struct variable instead! studentPtr: studentA: lastname: mark:

Passing a struct Pointer studentPtr: void readRecord ( Student *studentPtr ) { /* De-reference pointer here. */ } int main() { Student studentA; readRecord( &(studentA) ); return 0; } mark: studentA: lastname: lastname:

Passing a struct Pointer ITCS 102 Computer Programming Passing a struct Pointer studentPtr: mark: studentA: lastname: 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 Dr. B. KARLIK

Passing a struct Pointer ITCS 102 Computer Programming Passing a struct Pointer studentPtr: mark: studentA: lastname: 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 Dr. B. KARLIK

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

Example: Pointers to Structs-1 (cont) #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) #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-2 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"); printRecord( &(class[i]) ); return 0; marks4c.c