Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)

Similar presentations


Presentation on theme: "Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)"— Presentation transcript:

1 Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)

2 Structure This is the second Data structure that we will study and one of the powerful features of C language (made obselete by class in C++). Consider Student Data How to store names, age, address, city, CGPA for students Can u suggest how to create a database for it?

3 What is a Structure? A structure is an aggregate data structure used to keep different pieces of information together as a single data record. A structure is like an array except that each element can have a different data type, and the elements in a structure have names instead of subscript values

4 Declaring a struct Variable Syntax of the struct data type: struct name { type field_name1; type field_name2;... }; where – name is the name of the record type established by the struct, – each type field_name defines a field in the record, – the field definitions are separated by semicolons, and – the struct has a trailing semicolon.

5 Example of declaring a struct: /* declare a struct type (template) with name "Person_Record"*/ struct Person_Record { char name[20]; int age; }; /* Now, allocate memory */ struct Person_Record My_Record; /* a single record */ struct Person_Record PR_Array[100]; /* an array of records */

6 More about structures Normally structure is defined as global variable Its instances are created in main program or any other function The memory is not allocated when structure is defined. But when the instance of a structure is created then memory is reserved. Hence structure is like a cookie mold!  with no cookies unless you put the mixture of ingredients… Hence structure stand alone is a template… and it cannot be initialized. You need to Instantiate it or declare a variable of type structure

7 Initializing a Structure Variable To initialize a structure, follow the structure variable name with an equal sign, followed by a list of initialisers enclosed in braces. Example: struct Person_Record My_record = { "Julie Dagg", 32 };

8 Using a Structure Variable There are two ways to reference (access) the fields in a structure: The structure itself. A pointer to the structure.

9 I/O of Structures When you use cin/cout you must read/write a structure field-by-field. Emp.age = 20 Cin >> emp.age Cout << emp.age

10 Structures as Function Parameters The entire structure is copied to the function, i.e., a structure is passed by value. A pointer to a structure can be passed to achieve call-by-reference. Individual field of a structure can be passed as a real parameter to a function. It is possible for a function to return a structure or a pointer to a structure.

11 Examples: Passing an entire structure void func(Person_Record); //prototype of function... Person_Record My_Record; func(My_Record); /* call-by-value */ Passing a pointer to a structure void func(Person_Record *);... Person_Record My_record; func(&My_record); /* call-by-reference, pointer*/ Passing the address of a struct is faster, changes are reflected in My_Record

12 Nested Structures When one of the fields of a structure is itself a structure, it is called a nested structure. Struct DateofBirth { int years; int months; int day; }; Struct Emp { char[25] name; DateofBirth Dob; };

13 Self-Referential Structures (will do in detail in coming weeks) We can declare pointers to structures that have not yet been declared. These are called dynamic data structures. This permits us to create self-referential structures, as well as mutual-referential structures. Example: struct listnode { int nodedata; struct listnode *nextnode; };

14 Example Program Will do detailed program next time in the class! Here is a simple program

15 #include struct Date{ int mm; int dd; int yy; }; struct data{ char name[25]; int age; struct Date hireDate; }; void main(void) { data d[20]; //creates array of type data with limit 20 int i = 0; char ch; do { cout << "Please enter Name of the employee" << endl; cin >> d[i].name ; cout << "Please enter age" << endl; cin >> d[i].age ; cout << "Please enter date (dd,mm,yyyy)" << endl; cin >> d[i].hireDate.dd; cin >> d[i].hireDate.mm; cin >> d[i].hireDate.yy; cout << "Would like to enter more data? (y/n)" << endl; i++; cin >> ch; } while (ch != 'n'); for (int j=0; j < i; j++) { cout << endl << d[j].name; }}


Download ppt "Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)"

Similar presentations


Ads by Google