Presentation is loading. Please wait.

Presentation is loading. Please wait.

TMF1414 Introduction to Programming

Similar presentations


Presentation on theme: "TMF1414 Introduction to Programming"— Presentation transcript:

1 TMF1414 Introduction to Programming
Lecture 11: Structures

2 Objectives Introduction to Structures Defining Structures
Processing a structure User-defined Data types Passing structures to Functions Self-referential Structures

3 Introduction Array is a data type to store multiple values in the same data type. Structure in another way is like array but can store value from different data type An individual structure elements are referred as member ID NO Name Address GPA 1123 John Li 121, Jalan Blue 3.78

4 How Structure look like?
Syntax structure tag struct StructureTypeName { StructureMemberDeclarationsList }; /*end struct*/ List of declarations of its members Example struct faculty_struct { int faculty_idno; char faculty_name; int age; char gender; float GPA; }; /*end faculty_struct*/

5 Declaring Structure Variables
After declaring a structure type, we may declare variables that are of that type. A structure variable declaration requires these elements: The keyword struct The structure type name A list of variable names separated by commas A concluding semicolon struct_tag { datatype member1; datatype member2; datatype member3; }; IMPORTANT: Remember to put the “;” at the end of structure declaration

6 Faculty_record in this case is also called structure variables
Structures It is possible to combine the declarations of a structure type and a structure variable by including the name of the variable at the end of the structure type declaration. struct faculty_struct { int faculty_idno; char faculty_name; int age; char gender; float salary; } faculty_record; /*end faculty_struct*/ Faculty_record in this case is also called structure variables

7 What is the structure tag? How many members are there?
Example struct cdCollection { char title[25]; char artist[20]; int numSongs; float price; char dataBought[9]; } cd1, cd2, cd3; What is the structure tag? How many members are there? What are the member data types? What are the member names? How many structure variables are there? What are their name?

8 Nested Structures Since member of structure can be of any datatype
Therefore structure can also contain structure as its member struct tag1 { int member1; struct tag2 variable; }; Where structure tag2 had to be declared before tag1.

9 Processing a structure
Members of a structure are usually processed separately Therefore need to access the structure member individually Each member of the structure can be accessed by variable.member To access a member of a nested structure; variable.member.submember Where member is a structure type.

10 Example based on the structure type faculty_struct [slide 4], and the structure variable declaration struct faculty_struct faculty_record; faculty_record.faculty_idno = 1200; strcpy(faculty_record.faculty_name, “Farid”); faculty_record.age = 30; faculty_record.gender = ‘M’; faculty_record.salary = ; struct cdCollection cd1; strcpy(cd1.title, “ Red Moon Men”); strcpy(cd1.artist, “Sam and the sneeds”); cd1.numSongs=12; cd1.price=11.95 Strcpy(cd.datebought, “02/02/07”);

11 Example #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct cdCollection char title[25]; char artist[20]; int numSongs; float price; char dateBought[9]; } cd1; strcpy(cd1.title, "Red Moon Men"); strcpy(cd1.artist,"Sam and the sneeds"); cd1.numSongs = 12; cd1.price = 11.95; strcpy(cd1.dateBought, "02/02/07"); printf("Here is the CD information: \n\n"); printf("Title: %s \n", cd1.title); printf("Artist: %s \n", cd1.artist); printf("Songs: %d \n", cd1.numSongs); printf("Price: %.2f \n",cd1.price); printf("Date Bought: %s \n", cd1.dateBought); printf("\n"); system("PAUSE"); return 0; }

12 Initializing Structure Variables
In C it is possible to initialize structure variables at compile time. Compile time initialization of a structure variable requires the following elements: The keyword struct The structure type name The name of the structure variable The assignment operator, = A set of values for the members of the structure variable, enclosed in braces, and separated by commas A terminating semicolon

13 Example struct faculty_struct { int faculty_idno; char faculty_name;
int age; char gender; float salary; } faculty_record = {1200, “Farid”, 30, ‘M’, }; struct cdCollection { char title[25]; char artist[20]; int numSongs; float price; char dataBought[9]; } cd1={“Red Men”, “Sam and the sneeds”, 12, 11.95, “02/02/07”};

14 Initialization Rules The order of values for structure variable members enclosed in braces must match the order of members in the structure type declaration. It is possible to initialize only a subset of the members of a structure variable at compile time. However, that subset must always begin with the first member and continue until your target member without skipping any.

15 User-defined Data Types (typedef)
User can defined their own data type by typedef type new-type; Example: typedef int age; Typedef also can be use in structures where typedef struct {…} new-type;

16 Structure and Pointers
If variable represents a structure-type variable, then &variable represent the starting address of that variable Therefore a pointer can be declare for that particular structure, where struct {…} *ptvar; And the member of the structure can be accessed by ptvar->member;

17 Example #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int n = 3333; char t = 'c'; double b = 99.99; struct int *acct_no; char *acct_type; char *name; double *balance; } customer, *pc = &customer; customer.acct_no = &n; customer.acct_type = &t; customer.name = "smith"; customer.balance = &b; printf("\n%d\t%c\t%s\t%.2f\n", *customer.acct_no, *customer.acct_type, customer.name, *customer.balance); printf("\n%d\t%c\t%s\t%.2f\n", *pc->acct_no, *pc- >acct_type, pc->name, *pc->balance); printf("\n"); system("PAUSE"); return 0; }

18 Operations on Structure Variables
The following operations are permitted on structure variables: Copying the content of a structure variable to another structure variable, provided both are of the same structure type. Passing structure variables or their members to functions.

19 Copying content between structures
When you have two or more same structure type, where struct tag1{…} variable1, variable2; Then variable1 = variable2; is a valid operation

20 Passing structure to Functions
There are several ways to pass structure-type information to or from a function Data can be pass o function by individual member or the entire structures Individual structure members treated as ordinary single valued variable and can be passed to a function as arguments in the function call

21 Example of passing individual member
#include <stdio.h> #include <stdlib.h> #include <string.h> struct record{ char *name; int acct_no; char acct_type; float balance; }; int main() { float adjust(char *name, int acct_no, float balance); static record customer ={"Smith",333,'C',33.33}; printf("\nRecord before adjustment using individual pass"); printf("\n%s\t %d\t%c\t%.2f\n", customer.name, customer.acct_no, customer.acct_type, customer.balance); customer.balance = adjust(customer.name, customer.acct_no, customer.balance); printf("\nRecord after adjustment using individual pass"); printf("\n%s\t %d\t%c\t%.2f\n", customer.name, customer.acct_no, customer.acct_type, customer.balance); printf("\n"); system("PAUSE"); return 0; } float adjust(char *name, int acct_no, float balance) { float newbalance = 30.02; return (newbalance);

22 Example of Passing the whole Structure
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *name; int acct_no; char acct_type; double balance; }record; int main() { void adjust(record *pt); static record customer ={"Smith",333,'C',33.33}; printf("\nRecord before adjustment to function"); printf("\n%s\t %d\t%c\t%.2f\n", customer.name, customer.acct_no, customer.acct_type, customer.balance); adjust(&customer); printf("\nRecord after adjustment passing to function"); printf("\n%s\t %d\t%c\t%.2f\n", customer.name, customer.acct_no, customer.acct_type, customer.balance); printf("\n"); system("PAUSE"); return 0; } void adjust(record *pt){ pt->name = "Jones"; pt->acct_no = 9999; pt->acct_type = 'R'; pt->balance = 99.99; return;

23 Multiple block data in structures
One structure only can store one particular block of data as in one array. How about multiple block of data? In array, we can have multidimensional array. In structure, we can use structure array or structure of data that linked to each other through a pointer. Structure of data that linked to each other through a pointer also known as self- referential structures or linked list

24 Structure array #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *name; int acct_no; char acct_type; double balance; } record; int main() { static record customer[3] ={ {"Smith",333,'C',33.33}, {"Jones",666,'C',66.66}, {"Brown",999,'C',99.99} }; int acctn; record *pt; printf("\nAccount Record"); printf("\n "); for (acctn = 0; acctn <3; ++acctn) { pt = &customer[acctn]; printf("\n%s\t%d\t%c\t%.2f", pt->name, pt->acct_no, pt- >acct_type, pt->balance); } printf("\n"); system("PAUSE"); return 0;

25 Self-referential structures
A structures that has a links (pointer) to the next data that have same structure Or a structure that have a member that has the same structure as it owns. Example struct tag { datatype member1; struct tag *ptvar; } struct list_element { char *item; struct list_element *next; }

26 Self-referential structures allow data to linked together and provide flexible operation and organization on the data. The details of self-referential will be in Data Structure and Algorithms (TMC1433).

27 Union union student{ int id; char name[10]; float gpa; } u1;
Defined as similar way as structure. The only difference is that the union creates only one memory space. The size of the memory computed by the maximum size of its data members. union student{ int id; char name[10]; float gpa; } u1;

28 Difference between union and structure

29 Example u1.id=100; u1.gpa=3.75; Printf(“%d %f”,u1.id,u1.gpa); 100
Memory (10 bytes) Output: <garbage value> 3.75 Union variable saves the latest assignment of its members

30 Example u1.id=100; u1.gpa=3.75; printf(“%d %f”,u1.id,u1.gpa); 3.75
Memory (10 bytes) Output: <garbage value> 3.75 Union variable saves the latest assignment of its members

31 Example u1.id=100; u1.gpa=3.75; printf(“%d %f”,u1.id,u1.gpa); 3.75
Memory (10 bytes) Output: <garbage value> 3.75 Union variable saves the latest assignment of its members

32 Thank You


Download ppt "TMF1414 Introduction to Programming"

Similar presentations


Ads by Google