Lecture 19: Using structures in C Language Computer Programming Structures in C Language Lecture 19.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Exposure C++ Chapter XVI C++ Data Structures, the Record.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Copyright © 2002 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.
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
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?
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Lesson 11 Structured Data CS1 Lesson John Cole1.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
1 Chapter 11 Introducing the Class Pages ( )
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Introduction to Programming Lecture 39. Copy Constructor.
Structures in C.
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.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 32 Handling Selected Topics Again!! Structs and Arrays of Structs Special Lectures.
Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
Encapsulation by Subprograms and Type Definitions
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
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.
Structured Data Types array array union union struct struct class class.
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
1 CSC241: Object Oriented Programming Lecture No 06.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
11 Introduction to Object Oriented Programming (Continued) Cats.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
OUTLINE We will design a sample “Library” database in C language.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
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.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 10-1: Structure.
Chapter 11: Structured Data.
CS150 Introduction to Computer Science 1
Structures.
Dynamic Memory Allocation Reference Variables
Structures Lesson xx In this module, we’ll introduce you to structures.
Heterogeneous aggregate datatypes
CS148 Introduction to Programming II
Structured Data Types array union struct class.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Structures In C Programming By Rajanikanth B.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

Lecture 19: Using structures in C Language Computer Programming Structures in C Language Lecture 19

Lecture 19: Using structures in C Language Data Structures A data structure is a user's implementation of a data abstraction that does not already exist in the language. we can define a data structure to describe a group of related data, such as a "record" in a file. e.g. Student record (definition) Example (content of such a record) ID NumberFamily NameGiven NamesDate of Birth "Citizen""John Andrew""12/04/1989"

Lecture 19: Using structures in C Language Declaring Data Structures in C Syntax :- struct { ; };

Lecture 19: Using structures in C Language Example: Declaring a C struct struct Date { int day; int month; int year; }; members of the structure (sometimes called "fields") This merely declares a new data type called Date. You can then use it to create variables of type Date. Important:- Date is not a variable. There is no memory allocated for it. It is merely a type (like int, float, etc). structure name

Lecture 19: Using structures in C Language Defining a Structure Variable Syntax :- ;Examples: Date birthday; creates a variable called birthday of type Date. This variable has 3 components (members) : day, month, and year. Date today; creates another variable of type Date, also with component parts called day, month and year.

Lecture 19: Using structures in C Language Defining a Structure Variable Vs Defining a "normal" Variable int number; Date birthday; note the consistent format : ; NAME of the variables TYPE of the variables

Lecture 19: Using structures in C Language Initializing Structure Type with string members struct Name { char first[30]; Char last[30]; }; Name poet_name; strcpy(poet_name.first,Mirza); strcpy(poet_name.last,Ghalib); "Mirza" "Ghalib" Note : Values of the members need to be copied individually, AFTER the variable is created.

Lecture 19: Using structures in C Language Members of Different Types struct Student {... id;... name;... age;... gender ; }; student std; std.id = 1234; strcpy(std.name,Hassan Ali); std.age = 19; std.gender = 'M'; The members of a struct need not be of the same type. What should be the types of these members? 1234 Hassan Ali 19 M ali

Lecture 19: Using structures in C Language Creating structure of Library Database ISBNBook NameAuthor NamePublisherNumber of Copies Year of Publish 1293Network SecurityMartinWaley Data miningMuhammad ZakiWrox Data warehousingStephen BrobstMIT C ProgrammingM. KamberWaley41996 struct Library { int ISBN, copies, PYear; char bookName[30], AuthorName[30],PublisherName[30]; };

Lecture 19: Using structures in C Language Accessing Structure Members Library libraryVariable; cin >> libraryVariable.ISBN; cin >> libraryVariable.bookName; cin >> libraryVariable.AuthorName; cout << libraryVariable.ISBN << libraryVariable.bookName << libraryVariable.AuthorName; int tempISBN = libraryVariable.ISBN + 1; The parts of the variable are accessed, NOT the parts of the structure type. The variable is the only thing that has memory allocated to it. The dot is called the member operator

Lecture 19: Using structures in C Language Examples of Common Errors in Accessing Structures cout << bookName; // Error! // bookName is not a variable. It is only the name of a member in a structure cout << Library.bookName; // Error! // Library is not the name of a variable. It is the name of a type

Lecture 19: Using structures in C Language Common Errors in Accessing Structures (contd.) cout << libraryVariable; //cout does not know how to handle the variable libraryVariable, as it is not one of the built-in types. You have to give it individual bits of libraryVariable that it can recognize and handle. cout << libraryVariable.ISBN << libraryVariable.bookName; //this is OK

Lecture 19: Using structures in C Language Accessing Structure Variables (Example 1) void main (void) { struct Library { int ISBN, copies, PYear; char bookName[30], AuthorName[30], PublisherName[30]; }; Library libraryVariable; libraryVariable.ISBN = 1293; strcpy (libraryVariable.bookName, Network Security); strcpy (libraryVariable.AuthorName, Martin); strcpy (libraryVariable.PublisherName, Waley); libraryVariable.copies = 4; libraryVariable.PYear = 1998; cout << libraryVariable.ISBN << libraryVariable.bookName << libraryVariable.AuthorName << libraryVariable.PublisherName << libraryVariable.copies << libraryVariable.PYear; }

Lecture 19: Using structures in C Language Accessing Structure Variables (Example 1) void main (void) { struct Library { int ISBN, copies, PYear; char bookName[30], AuthorName[30], PublisherName[30]; }; Library libraryVariable1, libraryVariable2, libraryVariable3, libraryVariable4; Library LibraryArray [4]; // alternative and easiest way }

Lecture 19: Using structures in C Language Assignment to Structure Variable The value of a structure variable can be assigned to another structure variable of the same type, e.g : Library libraryVariable1, libraryVariable2; strcpy (libraryVariable1.bookName, C Programming); libraryVariable1.ISBN = 1293; libraryVariable2 = libraryVariable1; cout << libraryVariable2.bookName << libraryVariable2.ISBN; Assignment is the only operation permitted on a structure. We can not add, subtract, multiply or divide structures.

Lecture 19: Using structures in C Language Structures within Structures void main () { struct University {char Name [30]; char city [30]; Library libraryVariable; }; University universityVariable; strcpy (universityVariable.Name, UET); strcpy (universityVariable.city, ROME); universityVariable.libraryVariable.ISBN = 1293; strcpy (universityVariable.libraryVariable.bookName, C programming); }

Lecture 19: Using structures in C Language Accessing Structure in Structure cin >> universityVariable.libraryVariable.bookName; cin >> universityVariable.libraryVariable.ISBN; cout << universityVariable.libraryVariable.bookName << universityVariable.libraryVariable.ISBN;

Lecture 19: Using structures in C Language Passing Structure Variables as Parameters An individual structure member may be passed as a parameter to a function, e.g. : validLibraryData (libraryVariable.ISBN); An entire structure variable may be passed, e.g. : validLibraryData (libraryVariable); NOTE:- Structure variable is passed by value not by reference

Lecture 19: Using structures in C Language Example : Passing a Structure Member void validLibraryData (int ISBN); void main(void) { //assuming that Library structure has already defined Library libraryVarialbe; validLibraryData (libraryVariable.ISBN); } void validLibraryData (int ISBN) { cout << Library ISBN = << ISBN; }

Lecture 19: Using structures in C Language Example :Passing an entire Structure Example : Passing an entire Structure struct Library { int ISBN, copies, PYear; char bookName[30], AuthorName[30], PublisherName[30]; }; void validLibraryData (Library var1); void main (void) { Library libraryVariable; libraryVariable.ISBN = 1293; strcpy (libraryVariable.bookName, Network Security); strcpy (libraryVariable.AuthorName, Martin); validLibraryData (libraryVariable); } void validLibraryData (Library var1) { cout << ISBN = << var1.ISBN << \n; cout << Book name = << var1.bookName << \n; }

Lecture 19: Using structures in C Language Returning a Structure Variable struct Library { int ISBN, copies, PYear; char bookName[30], AuthorName[30], PublisherName[30]; }; Library inputBookInformation (void); void main (void) { Library libraryVariable1; libraryVariable1 = inputBookInformation ( ); cout << libraryVariable1.ISBN << libraryVariable1.bookName; } Library inputBookInformation (void) { Library var1; var1.ISBN = 1293; strcpy (var1.bookName, Network Security); strcpy (var1.AuthorName, Martin); }

Lecture 19: Using structures in C Language Pointers to structure variables Pointers of structure variables can be declared like pointers to any basic data type Library var1, *ptrToLibrary; ptrToLibrary = &var1; Members of a pointer structure type variable can be accessed using (->) operator ptrToLibrary->ISBN =20; strcpy( ptrToLibrary->bookName, C Programming);

Lecture 19: Using structures in C Language Pointers to structure variables (Example 1) void main (void) { Library libraryVariable1, *PtrToLibrary; libraryVariable.ISBN = 1293; strcpy (libraryVariable.bookName, Network Security); strcpy (libraryVariable.AuthorName, Martin); strcpy (libraryVariable.PublisherName, Waley); libraryVariable.copies = 4; libraryVariable.PYear = 1998; PtrToLibrary = &libraryVariable1; PtrToLibrary.ISBN = 3923; PtrToLibrary.copies = 10; cout << The values are << libraryVariable1.ISBN <<, ISBN; } Output: The values are 3923, 3923

Lecture 19: Using structures in C Language Pass by Reference Pass by Reference structure variables to Functions (Example 1) void Function1 (Library *ptr); void main (void) { Library var1; Function1 (&var1); cout << var1.ISBN << var1.bookName << var1.AuthorName; } void Function1 (Library libraryVariable) { libraryVariable.ISBN = 1293; strcpy (libraryVariable.bookName, Network Security); strcpy (libraryVariable.AuthorName, Martin); strcpy (libraryVariable.PublisherName, Waley); libraryVariable.copies = 4; libraryVariable.PYear = 1998; } Output: 1293 Network Security Martin