Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Structures

Department of IT Engineering

Lecturer: Kor Sokchea

C Programming
Exercise
 TA Contact:
Name: Mr. Kheang Kim Ang
Email: kimangkheang@gmail.com
Phone 086 348378
Room: 311

 Office Hour (ម៉ោ ងដែលទំនេរដែលសិស្សអាចមកពិភាក្សាបញ្ហា ដែលមាននៅក្នុងមុខវិជ្ជា C Programming)

Monday Tuesday Wednesday Thursday Friday


7:30 – 8:30 9:00 – 10:00 16:00 – 17: 00 9:00 – 10:00 9:00 – 10: 00

 សូមផ្ញើ Homework ទៅកាន់អាស័យដ្ខា នអ៊ីមែលខាងលើ

 Subject Email: c_program_yourname_labgroup_homeworknumber

2
Content
 Structures

 Declaration of Structure

Lectured by: Kor Sokchea 3


Structures
 A Structure is a collection of related data items, possibly of different types

 A structure type in C and C++ is called struct

 A struct is heterogeneous in that it can be composed of data of different types

 In contrast, array is homogeneous since it can contain only data of the same type

 Help to organize complicated data, particularly in large programs

 Permit a group of related variables to be treated as a unit instead of as separate


entities

Lectured by: Kor Sokchea 4


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, …

Lectured by: Kor Sokchea 5


Declaration of Structures
 In C, structure declaration uses keyword struct

 A structure type is usually defined at the beginning of a program just after the main()
statement
 Structure tag is optional Structure tag or identifier
Keyword struct use for
declaring structure in c

struct struct_identifier{
datatype identifier_list; Each identifier
datatype identifier_list; defines a member
of the structure.
----------
----------
};
Lectured by: Kor Sokchea 6
Declaration of Structures
 Example
struct Student{ struct Date{
int id; int day;
char name[30]; int month;
char gender; int year;
int age; };
float score The “Date” structure has
}; 3 members, day, month,
year
The “Student” structure has 5
members, id, name, gender, age,
score

Lectured by: Kor Sokchea 7


Declaration of Structures
 A structure declaration defines a type

struct circle{
int x;
int y;
float radius;
};
 With structure identifier, it can defines new structure variable

struct circle c1, c2, c3;

Lectured by: Kor Sokchea 8


Declaration of Structures
 A structure declaration defines a type

 With structure identifier, it can defines new structure variable


Syntax 1: Syntax 2:

struct circle{ struct circle{


int x; int x;
int y; int y;
float radius; = float radius;
}; } c1, c2, c3;
// Declaration of a variable of // Declaration of a
struct type
variable of struct type
struct circle c1, c2, c3;
Lectured by: Kor Sokchea 9
Structures Initialization
 A structure can be initialized by following its definition with a list of
struct circle{
int x;
int y;
float radius;
};
 Initializing structure with list  Initializing structure with
individual element
struct circle c = { struct circle c;
30, 40, 10.0 c.x = 30;
}; c.y = 40;
c.z = 10.0;

Lectured by: Kor Sokchea 10


Accessing Structures Member
 A member of a particular structure is accessed using the expression as below

struct-identifier.member;

Example: Having triangle structure Accessing member of triangle structure


struct triangle{ t.x = 10;
int x; t.y = 20;
int y; t.radius = 5.0;
float radius;
}; printf("x: %d", t.x);
printf("y: %d", t.y);
struct triangle t; printf("radius: %d", t.radius);

Lectured by: Kor Sokchea 11


Using Typedef keyword
 C language allows a programmer to rename data types using the keyword typedef

 Example: typedef unsigned int uint;

uint x, y;
 In the above example, a new type unsigned integer as uint have defined

 Then, uint can be used in program as any native data type, and declare other
variables with its data type.

Lectured by: Kor Sokchea 12


Using Typedef keyword
 typedef can be used with struct as well

 Example:

typedef struct car{


char brand[10];
int colorCode;
int price;
}Cars;
 Then you can use the newly defined data type, as in the following example:
Cars bmw, mercede;

Lectured by: Kor Sokchea 13


Nested Structure
 We can nest structures inside structure

 Example: struct Point{ (p.x, p.y)


double x, y;
};
(line.p2.x, line.p2.y)
Point p;
(line.p1.x, line.p1.y)
struct Line{
Point p1, p2;
(trig.p2.x, trig.p2.y)
};

Line line;

struct Triangle{ (trig.p3.x, trig.p3.y)


Point p1, p2, p3;
};
(trig.p1.x, trig.p1.y)
Triangle trig;

Lectured by: Kor Sokchea 14


Structure and Function
 The only legal operations on a structure are
 Copying it
 Assigning to it as a unit
 Taking its address with &
 Accessing its members.
For example: we have a struct circle as below
typedef struct {
float x;
float y;
float radius;
}Circle;

Lectured by: Kor Sokchea 15


Structure and Function
 Returning values of struct from the function
Circle makeCircle(float x, float y, float radius){
Circle circle;
circle.x = x;
circle.y = y;
circle.radius = radius;
return circle;
}
 Passing struct as argument to function

void getCircle(Circle circle){


printf("Circle x:%f", circle.x);
printf("Circle y:%f", circle.y);
printf("Circle radus:%f", circle.radius);
}

Lectured by: Kor Sokchea 16


Array of Structure
 An ordinary array: One type of data

0 1 2 … 98 99
 An array of structs: Multiple types of data in each array element

0 1 2 … 98 99

Lectured by: Kor Sokchea 17


Array of Structure
 C does not limit a programmer to storing simple data types inside an array

 User defined structures too can be elements of an array

 Example: we have a class care define in slide 13


//defines an array called Car that has 5 elements
Cars car[5];
 Each element inside the array will be of type struct car

brand brand brand brand brand


color color color color color
price price price price price

car[0] car[1] car[2] car[3] car[4]

Lectured by: Kor Sokchea 18


Array of Structure
 Referencing an element in the array is quite simple
car[0].brand = 'Bmw'; car[2].brand = 'Lexus';
car[0].color = 1; car[2].color = 89;
car[0].price = 100000; car[2].price = 40000;

car[1].brand = 'Mercedes'; car[3].brand = 'Audi';


car[1].color = 125; car[3].color = 60;
car[1].price = 80000; car[3].wheel = 70000;

brand Bmw Mercedes Lexus Audi


color 1 125 89 60
price 4 8 4 10

car[0] car[1] car[2] car[3] car[4]

Lectured by: Kor Sokchea 19


Initialization of Array of Structure
 Initialisation of structure arrays is similar to initialization of multidimensional arrays
Cars car[5]= {
{'Bmw', 25, 200000},
{'Mercedes', 80, 150000}
};

brand Bmw Mercedes


color 25 80
price 200000 150000

car[0] car[1] car[2] car[3] car[4]

Lectured by: Kor Sokchea 20


Literature
 Kernighan, B. W., & Ritchie, D. M. (1988). The C programming language (Vol. 2).
Englewood Cliffs: prentice-Hall.

21

You might also like