User Defined Data Types - Structures in C CHAPTER 4.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
EENG212 Algorithms and Data Structures
1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
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.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Foundation Studies Course M.Montebello Records Foundation Studies Course.
Structured Data Types array array union union struct struct class class.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Computer Programming Control Structure
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Struct Data Type in C++ What Are Structures?
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
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 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Advance Use of Structures
Chapter 10-1: Structure.
User Defined Data Types - Structures in C
Structs versus Classes
TMF1414 Introduction to Programming
Visit for more Learning Resources
Buy book Online -
S. Kiran, PGT (CS) KV, Malleswaram
Structures.
Structures In C Programming By Rajanikanth B.
The Pointers of Structures
CSCE 206 Lab Structured Programming in C
Structures, Unions, and Enumerations
Presentation transcript:

User Defined Data Types - Structures in C CHAPTER 4

C.09 1 Structures » An ARRAY allow the programmer to define variables that associated several data items of the same kind. » A STRUCTURE allow the programmer to define variables that associated several data items of different kind. » The STRUCTURE in C is similar to the RECORD in languages such as Pascal (Delphi), COBOL. » Each student Identity structure (record) is containing some members (fields) like, “student_no”, “name”, “birth_date”, …)

C.09 2 Defining a Structure » A structure is defined in terms of members (fields). struct tag { member 1; member 2; … member n; }; » struct is a key word. » tag is a name that identify the structures having this composition. » member 1, …, member n are individual member declarations. » Members can be ordinary variables, pointers, arrays or other structures. » Member names within the particular structure must be distinct from one another.

C.09 3 Defining a Structure » Member names can be same as the name of a variable defined outside of the structure, or with the name of a member defined under a different structure (not preferred). » Once the composition of a structure has been defined, individual structure type variables can be declared as follows: struct tag variable 1, variable 2, …, variable n; » Where variable 1, …, variable n are variables that are declared in struct tag data type.

C.09 4 Defining a Structure » Example : struct account { int acct_no; char acct_type; char acct_name[81]; float balance; }; struct account oldcustomer, newcustomer; The oldcustomer and the newcustomer are variables of type account. (They are called structure-type variables). » It is possible to combine the declaration of the structure composition with that of the structure-variables. struct account { int acct_no; char acct_type; char acct_name[81]; float balance; } oldcustomer, newcustomer;

C.09 5 Defining a Structure » A structure may also be defined as the member of another structure. » In this case the declaration of the embedded structure must come before the declaration of the container structure. » e.g : struct date { int day; int month; int year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date lastpayment; } oldcustomer, newcustomer;

C.09 6 Defining a Structure » On the following example, the structure account is containing another structure date as one of its member. » e.g : struct date { int day, month, year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date firstpayment; struct date lastpayment; } oldcustomer, newcustomer;

C.09 7 Initializing a Structure struct date { int day, month, year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date lastpayment; } ; struct account customer = {12345, ‘R’, “Salaries”, 500.3, 28, 5, 2002}; » customer is a structure variable of type account. » Assignments: acct_no = 12345, acct_type = ‘R’, acct_name = “Salaries” balance = 500,3 lastpayment (day = 28, month = 5, year = 2002)

C.09 8 Array of Structure » It is possible to define an array of structures. struct date { int day, month, year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date lastpayment; } customer[100]; » In this example, the customer is declared as an array of struct account with 100 elements.

C.09 9 Array of Structure » An array of structures can be initialized just as any othe array can be. struct date { char name[21]; int day, month, year; }; struct date birthdate[3] = { “Ali”, 20, 7, 1974, “Ayse”, 18, 1, 1982, “Fatma”, 8, 12, 1983 }; or struct date birthdate[3] = { {“Ali”, 20, 7, 1974}, {“Ayse”, 18, 1, 1982}, {“Fatma”, 8, 12, 1983} };

C Processing a Structure » The members of the structures must be accessed and processed separately. variable.member » variable : name of the structure-type variable. » member : The name of the member within the structure. » The period (. ) is an operator of highest priority and has left-to-right associatively.

C Processing a Structure e.g: struct date { int day, month, year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date lastpayment; } customer; … customer.acct_no = 1221; // sets cursomer’s account no to 1121 customer.balance = 0; // sets cursomer’s balance to 0 printf(“%s”,customer.acct_name); // displays the cursomer’s account name ++customer.balance; // Increments the customer’s balance by one

C Processing a Structure e.g (continuing) : scanf(“%d”,&customer.lastpayment.year); // reading the cursomer’s last payment date year printf(“Last Payment : %2d-%2d-%d “, customer.lastpayment.day, customer.lastpayment.month, customer.lastpayment.year); // displays the last payment date of the customer printf(“%c”,customer.acct_name[2]); // displays the 3 rd character of the cursomer’s account name customer.acct_type=‘I’; // Sets the account type of the customer to ‘I’. total + = customer.balance; // Adds the balance of the customer to the variable “total”

C Processing a Structure e.g : struct date { int day, month, year; }; struct account { int acct_no; char acct_type; char acct_name[81]; float balance; struct date lastpayment; } customer[100]; … customer[13],balance=0;// Sets the balance of the 14 th customer to 0. ++customer[5].lastpayment.day; // Incrementing the last payment date by one day of the 6 th customer.

C Example Problem: Write a program including structures to read mt, final and quiz scores of the class with 25 students and will display the score list of the class with total grade of each student and the average of the class. Note that, each student has to have a record with the following fields in the class: std_no (Student Number) std_name (Student Name) mt(Midterm Score) final(Final Score) quiz(Quiz Score) grd(Total Grade) The weights of the scores are given as: 30 % Midterm, 50% Final, and 20% Quiz

C Example #include 1. #include struct student { char stdno[7]; char name[21]; int mt, final, quiz; float grd; }; void main() { struct student c213[2]; int totscores[3]={0,0,0}, i; float totgrd=0; char blank[2]; // Reading Student Information2. for (i=0; i< 2; i++) { clrscr(); printf("\n%4d. Student Data Entry\n\n", i+1); printf("\n Student No :"); gets(c213[i].stdno); printf("\n Student Name :"); gets(c213[i].name); printf("\n Midterm Score:"); scanf("%d",&c213[i].mt); printf("\n Final Score:"); scanf("%d",&c213[i].final); printf("\n Quiz Score:"); scanf("%d",&c213[i].quiz); gets(blank);

C Example c213[i].grd = 0.30*c213[i].mt *c213[i].final *c213[i].quiz; totscores[0]+=c213[i].mt; // Midterm Scores Total totscores[1]+=c213[i].final; // Final Scores Total totscores[2]+=c213[i].quiz; // Quiz Scores Total totgrd+= c213[i].grd; // Grades total } // Displaying the Class Scores List clrscr(); printf("\n Class Scores List\n\n"); printf("\n STD_NO NAME MT FIN QUI GRADE"); printf("\n==================================================="); for (i=0; i< 2; i++) { printf("\n%7s %20s %3d %3d %3d %6.2f", c213[i].stdno,c213[i].name, c213[i].mt, c213[i].final, c213[i].quiz, c213[i].grd); }

C Example 4. // The Score Averages part of the List printf("\n==================================================="); printf("\nAverages %3d %3d %3d %6.2f", totscores[0]/2, totscores[1]/2, totscores[2]/2, totgrd/2); getch(); }