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

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

Embed Size (px)

Citation preview

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

Lecture 19: Using structures in C Language

Computer ProgrammingComputer Programming

Structures in C LanguageStructures in C LanguageLecture 19Lecture 19

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

Lecture 19: Using structures in C Language

Data StructuresData 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 Number Family Name Given NamesDate of Birth

11112222 "Citizen" "John Andrew""12/04/1989"

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

Lecture 19: Using structures in C Language

Declaring Data Structures in CDeclaring Data Structures in C

Syntax :-Syntax :- struct <structName>

{

<type> <memberName1>;

<type> <memberName2>;

<type> <memberName3>;

......

};

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

Lecture 19: Using structures in C Language

Example: Declaring a C structExample: 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

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

Lecture 19: Using structures in C Language

Defining a Structure VariableDefining a Structure Variable

Syntax :-Syntax :- <structName> <variableName>;

Examples: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.

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

Lecture 19: Using structures in C Language

Defining a Structure Variable Vs Defining a Structure Variable Vs Defining a "normal" VariableDefining a "normal" Variable

int number;

Date birthday;

note the consistent format : <type> <variableName>;

NAME of the variables

TYPE of the variables

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

Lecture 19: Using structures in C Language

Initializing Structure Type with Initializing Structure Type with string membersstring 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.

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

Lecture 19: Using structures in C Language

Members of Different TypesMembers 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 thesemembers?

1234 “Hassan Ali” 19 ‘M’ali

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

Lecture 19: Using structures in C LanguageCreating structureCreating structure of Library of Library DatabaseDatabaseISBN Book Name Author Name Publish

erNumber of Copies

Year of Publish

1293 Network Security

Martin Waley 4 1998

9382 Data mining Muhammad Zaki

Wrox 6 2003

9993 Data warehousing

Stephen Brobst

MIT 8 2003

3423 C Programming

M. Kamber Waley 4 1996struct Library{

int ISBN, copies, PYear;char bookName[30], AuthorName[30],PublisherName[30];

};

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

Lecture 19: Using structures in C Language

AccessingAccessing Structure Members 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

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

Lecture 19: Using structures in C Language

Examples of Common ErrorsExamples of Common Errors in in Accessing StructuresAccessing 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

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

Lecture 19: Using structures in C Language

Common ErrorsCommon Errors in Accessing in Accessing Structures (contd.)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

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

Lecture 19: Using structures in C Language

Accessing Structure VariablesAccessing Structure Variables (Example 1)(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;

}

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

Lecture 19: Using structures in C Language

Accessing Structure VariablesAccessing Structure Variables (Example 1)(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}

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

Lecture 19: Using structures in C Language

AssignmentAssignment to Structure to Structure VariableVariable

• 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.

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

Lecture 19: Using structures in C Language

Structures within Structures within StructuresStructures

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

}

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

Lecture 19: Using structures in C Language

Accessing Structure in Accessing Structure in StructureStructure

cin >> universityVariable.libraryVariable.bookName;

cin >> universityVariable.libraryVariable.ISBN;

cout << universityVariable.libraryVariable.bookName << universityVariable.libraryVariable.ISBN;

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

Lecture 19: Using structures in C Language

Passing Structure VariablesPassing Structure Variables as as ParametersParameters

• 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

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

Lecture 19: Using structures in C Language

Example :Example : Passing a Structure Passing a Structure MemberMembervoid validLibraryData (int ISBN);void main(void){ //assuming that Library structure has already definedLibrary libraryVarialbe;validLibraryData (libraryVariable.ISBN);

}void validLibraryData (int ISBN){cout << “Library ISBN = “ << ISBN;

}

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

Lecture 19: Using structures in C Language

Example :Example : Passing an entire Passing an entire StructureStructurestruct 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”;}

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

Lecture 19: Using structures in C Language

ReturningReturning a Structure Variable a Structure Variablestruct 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”);}

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

Lecture 19: Using structures in C Language

PointersPointers to structure variables 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”);

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

Lecture 19: Using structures in C Language

Pointers to structure variablesPointers to structure variables (Example 1)(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 << “ , ” << PtrToLibrary->ISBN;}

Output: The values are 3923 , 3923

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

Lecture 19: Using structures in C LanguagePass by ReferencePass 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