Summary

This document provides an introduction and explanation of data structures in C, focusing on structs and their use cases. It includes examples for defining and using structs, illustrating their versatility in grouping related data items of different types. The document also explores the concept of typedefs for data types.

Full Transcript

Data Structure User Defined Data Types (typedef) The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, The data “type” name intShakil can now be used in declarations in exactly the same way that the data type int can be...

Data Structure User Defined Data Types (typedef) The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, The data “type” name intShakil can now be used in declarations in exactly the same way that the data type int can be used: User Defined Data Types (typedef) The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int intShakil; makes the name intShakil a synonym (or alias) for the data type int. The data “type” name intShakil can now be used in declarations in exactly the same way that the data type int can be used: intShakil a, b, len ; intShakil numbers ; Data Structures Data Structures Data Structures A Structure is a collection of related data items, possibly of different types. A structure type in C is a user-defined data type. It is always called struct. Data Structures A struct is heterogeneous in that it can be composed of data of different types. In contrast, an array is homogeneous since it can contain only data of the same type. Structures Structures hold data that belong together. Examples: Student record: student id, name, major, gender, start year, … Bank account: account number, name, currency, balance, … Address book: name, address, telephone number, … Structures Individual components of a struct type are called members (or fields). Members can be of different types (simple, array or struct). A struct is named as a whole while individual members are named using field identifiers. (myName.last – myName.first) Complex data structures can be formed by defining arrays of structs. Data structures Data has a structure to it. Different pieces of data have different structures. Examples: The high score in a game is a single integer The top 10 scores in a game is a sorted array of integers A student is a string for their name, an int for their student ID, a string for their email, a double for their course grade... A course enrollment list is an array of students Data structures int High score: 9001 int int int int int int int int int int Top 10 scores: 9001 8777 8456 8000 7433 7212 6889 5023 5022 5012 int char array char array double Student: 2781 "Terry" "[email protected]" 97.8 id name email grade Course enrollment list: student student... Definition and Declaration of Struct Definition and Declaration of Struct Definition and Declaration of Struct Definition and Declaration of Struct Definition and Declaration of Struct Declaring Structures (struct) struct myStruct { int label; char letter; char name; }; Typedef & Struct Often, typedef is used in combination with struct to declare a synonym (or an alias) for a structure: typedef struct myStruct { int label; char letter; char name; } myStruct_type; struct basics Example: struct Date { The “Date” structure int day; has 3 members, int month; int year; day, month & year. } ; struct examples Example: struct StudentInfo{ int Id; int age; The “StudentInfo” char Gender; structure has 4 members double CGA; }; of different types. Example: struct StudentGrade{ char Name; char Course; The “StudentGrade” int Lab; int Homework; structure has 5 int Exam; members of }; different array types. struct examples Example: struct BankAccount{ char Name; The “BankAcount” int AcountNo; structure has simple, double balance; array and structure Date Birthday; types as members. }; Example: struct StudentRecord{ The “StudentRecord” char Name; structure has 4 int Id; char Dept; members. char Gender; }; struct examples Example: struct BankAccount{ char Name; The “BankAcount” int AcountNo; structure has simple, double balance; array and structure Date Birthday; types as members. }; Example: struct StudentRecord{ The “StudentRecord” char Name; structure has 4 int Id; char Dept; members. char Gender; }; struct examples DEMO struct basics struct StudentRecord{ char Name; int Id; char Dept; char Gender; }; Example: struct StudentRecord Student1, Student2; Name Name Student1 Student2 Id Gender Id Gender Dept Dept Student1 and Student2 are variables of StudentRecord type. Ex. 1: struct basics The members of a struct type variable are accessed with the dot (.) operator: Student1 Name Example: Id Gender strcpy(Student1.Name, "Chan Tai"); Dept Student1.Id = 12345; strcpy(Student1.Dept, “SE"); Chan Tai Student1.gender = 'M'; 12345 M } SE Ex. 2: struct-to-struct assignment The values contained in one struct type variable can be assigned to another variable of the same struct type. Student1 Chan Tai Man Example: 12345 M SE Student2 = Student1; Chan Tai Man 12345 M Student2 SE Ex. 3-5: Nested structures We can nest structures inside structures. Examples: (P.x, P.y) struct point{ (L.p2.x, L.p2.y) double x, y; }; (L.p1.x, L.p1.y) struct point P; struct line{ (T.p2.x, T.p2.y) point p1, p2; }; struct line L; (T.p3.x, T.p3.y) struct triangle{ point p1, p2, p3; }; (T.p1.x, T.p1.y) struct triangle T; Ex. 3-5: Nested structures We can nest structures inside structures. struct line{ point p1, p2; }; (L.p2.x, L.p2.y) struct line L; (L.p1.x, L.p1.y) line p1 p2 x y x y Ex. 3-5: Nested structures Assign values to the variables P, L, and T using the picture: point P; line L; (4, 11) triangle T; (10, 9) Example5.c (2, 7) (6, 5) (8, 3) (2, 0) Ex. 3-5: Nested structures point P; line L; triangle T; (4, 11) P.x = 4; P.y = 11; (10, 9) L.p1.x = 2; L.p1.y = 7; (2, 7) L.p2.x = 10; L.p2.y = 9; (6, 5) T.p1.x = 2; T.p1.y = 0; T.p2.x = 6; T.p2.y = 5; (8, 3) T.p3.x = 8; T.p3.y = 3; (2, 0) Structs with Functions Structs with Functions Functions can return one value Functions can return/update more than one value using pointers Functions can return more than one value using structs! Structs with Functions - Motivation More than one attribute for abstract data Example – Pixel of an image : Red, Green, Blue color components RGB Image with 4 x 4 pixels RGB Image Data (4 x 4 pixels) RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB Structs with Functions Represent such data in C C provides struct syntax for grouping attributes of data in memory C struct defines a new data type Struct members can be any primitive data types and/or struct data types RGB Image Data (4 x 4 pixels) struct Pixel { RGB RGB RGB RGB RGB RGB RGB RGB Attributes are unsigned char red; RGB RGB RGB RGB unsigned char green; RGB RGB RGB RGB Struct members unsigned char blue; }; Structs with Functions - Example Structs with Functions - Example Structs with Functions - Example Structs with Functions - Example Structs with Functions - Example Structs with Functions - Example Structs with Functions - Example

Use Quizgecko on...
Browser
Browser