Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Structure part 1 & File Processing. 2 Structures.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Learners Support Publications Classes and Objects.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
User Defined Data Types
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
TMF1414 Introduction to Programming
Visit for more Learning Resources
Buy book Online -
Buy book Online -
Chapter 10: Records (structs)
EKT150 : Computer Programming
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Structures In C Programming By Rajanikanth B.
Structures Declarations CSCI 230
Getting Started With Coding
Presentation transcript:

Lecture 10: Structures

Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions Parameters Arrays of structures and Structures of array

Introduction Structures Collections of related data items called components(or members) that are not necessarily of the same data type Commonly used to define records to be stored in files Usually the collections of related data item are characterisctics in an object E.g Object Characteristics Book price, number of pages, year published Car price, year, model, colour Student name, matric_no, semester

Structure Definition Syntax struct StructureTypeName { structure member declaration list };  E.g. struct book { float price; int numpages; int year; };

Structure Definition struct information A struct cannot contain an instance of itself Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory Instead creates a new data type used to define structure variables

Structure Declaration After defining a structure type, we may declare variables that are of that type. A structure variable declaration requires these elements : keyword struct structure type name a list of variables names separated by commas A concluding semicolon E.g. struct book book1;

How does it look like in my program? #include struct book { float price; int numpages; int year; }; int main( ) { struct book book1; …… } #include struct book { float price; int numpages; int year; } book1; int main( ) { …… } or

Initializing structures e.g. struct book book1={ 25.50, 690, 2005 }; OR book1.price = 25.50; book1.numpages=690; book1.year = 2005;

Operations on structure members OperationNormal variableStructure member Read from user scanf( “ %f ”, &price);scanf( “ %f ”, &book1.price); Assign valueprice = 25.50;book1.price = 25.50; Print as output printf( “ %f\n ”, price);printf( “ %f\n ”, book1.price); Copy to a variable newprice = price;newprice=book1.price; Pass value to a function if(fun1(price) > 5)if(fun1(book1.price) > 5)

Multiple structure variables struct book my_book, his_book, her_book; my_book  price  numpages  year  price  numpages  year  price  numpages  year his_book her_book

Multiple structure variables e.g. struct book my_book, his_book, her_book; my_book. price = 25.50; her_book.price = 10.50; if(my_book.price > her_book.price) printf(“My book is more expensive than hers\n”); else printf(“My book is cheaper than hers\n”);

Sample program #include struct book { float price; int numpages; int year; }; int main() { struct book my_book = {25.50,690,2005}; struct book her_book; printf("Enter book price : "); scanf("%f", &her_book.price); printf(“\n Enter number of pages : "); scanf("%d", &her_book.numpages); printf(“\n Enter year published : "); scanf("%d", &her_book.year); printf(“\n My book :\n"); printf("%.2f\t%d\t%d\n", my_book.price, my_book.numpages, my_book.year); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", her_book.price, her_book.numpages, her_book.year); if(my_book.year > her_book.year) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); return 0; }

Structures as function parameters  Like variables of any other data type, structure variables can be used as formal and actual function parameters.  In passing structure variables by value to a function, the operating systems makes a copy of the entire structure in the data area of the called function.

Structures as function parameters …… float compute_price(struct book bk); //function prototype … int main( ) {struct book bookC; …………… new_price = compute_price(bookC);//function call …… } float compute_price(struct book bkC) {……. bkC.price=bkC.price + tax; …… return(bkC.price); }

Structures as function parameters A nonvoid function of a structure type can return a structure of that structure type under the function’s name if we use a return statement in the function. int main( ) {………. struct book b; b = read( ); ………. } struct book read( )//function definition {struct book bk; printf(“Enter price:”); scanf(“%f”, &bk.price); ….. return(bk); }

Structures as function parameters If all members of a structure variable are not needed for a function to perform its task, we can pass only the required members. However, we must specify structure members using the component selection operator. int modify_year(int a, int b, int year); //function prototype …….. int main( ) {struct book bkC; ……………. avg_year=calc_year(aa, bb, bkC.year);//function call ……. }

Arrays of Structure Suppose a company has 50 full-time employees. struct employeeType { char firstName[20]; char lastName[20]; int personID; char deptID[10]; double yearlySalary; double monthlySalary double yearToDatePaid; double monthlyBonus; }; struct employeeType employees[50];

How it looks like

How to access?? int counter; for(counter = 0; counter < 50; counter++) { scanf(“%s %s %d %s %lf”, &employees[counter].firstName, &employees[counter].lastName, &employees[counter].personID, &employees[counter].deptID, &employees[counter].yearlySalary); employees[counter].monthlySalary = employees[counter].yearlySalary/12; employees[counter].yearToDatePaid = 0.0; employees[counter].monthlyBonus = 0.0; }

How to access?? Suppose that for a given month, the monthly bonuses are already stored in each employee’s record, and we want to calculate the total amount to be received by each employee monthly. The following loop computes and prints the employee’s total amount for the month: double total_monthly_paid; for(counter = 0; counter < 50; counter++) { printf(“%s %s”, employees[counter].firstName, employees[counter].lastName); total_monthly_paid = employees[counter].monthlySalary + employees[counter].monthlyBonus; }

Structures as function parameters It is possible to pass structure variables using pointers int main() {struct book b; ……… read(&b);//function call } void read(struct book *bk) { printf(“Enter price:”); scanf(“%f”, &bk->price); printf(“Enter numpages:”); scanf(“%d”, &bk->numpages); ……. }

Sample program #include struct book { float price; int numpages; int year; }; struct book read(); void print(struct book, struct book); void compare(int, int); int main() { struct book my_book = {25.50,690,2005}; struct book she_book; she_book=read(); print(my_book, she_book); compare(my_book.year, she_book.year); return 0; } struct book read() { struct book her_book; printf("Enter book price : "); scanf("%f", &her_book.price); printf(“\n Enter number of pages : "); scanf("%d", &her_book.numpages); printf(“\n Enter year published : \n"); scanf("%d", &her_book.year); return(her_book); } void print(struct book my_book, struct book her_book) { printf("My book :\n"); printf("%.2f\t%d\t%d\n", my_book.price, my_book.numpages, my_book.year); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", her_book.price, her_book.numpages, her_book.year); } void compare(int my_year, int she_year) { if(my_year > she_year) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); }

Arrays in Structs Example const arraySize = 5; struct listType { int listElem[arraySize]; //array containing the list int listLength; //length of the list }; struct listType list;

How it looks like struct listType listElem listLength

Structs within a struct struct date { int day; int month; int year; }; struct student { int id_no; char name[20]; char address[20]; char combination[3]; int age; structure date def; structure date doa; }oldstudent, newstudent;

Structs within a struct struct salary { char name[20]; char department[10]; int basic_pay; int transport_allowance; int rental_allowance; int entertainment _allowance ; } employee; struct salary { char name [20]; char department[10]; int basic_pay; struct { int transport_allowance; int rental_allowance; int entertainment _allowance; } allowance; } employee;

Structs within a struct : How to access?? The salary structure contains a member named allowance which itself is a structure with 3 members. The members contained in the inner, structure namely transport, rental, and entertainment can be referred to as : employee allowance. transport employee. allowance. rental employee. allowance. entertainment

Structs within a struct : How to access?? An inner-most member in a nested structure can be accessed by chaining all the concerned. Structure variables (from outer-most to inner-most) with the member using dot operator. The following being invalid: employee. allowance // actual member is missing employee. rental // inner structure variable is missing

End of Lecture 10 – Q & A