1 c language structures. 2 topics concept of a structure concept of a structure structures in c...
TRANSCRIPT
11
C LanguageC Language
StructuresStructures
22
TopicsTopics Concept of a structureConcept of a structure
Structures in cStructures in c
Structure declarationStructure declaration
Structure variable definitionStructure variable definition
Structure usageStructure usage Member accessMember access Allowed operationsAllowed operations FunctionsFunctions
33
Concept of a StructureConcept of a Structure A programmer-defined data type
Logical grouping of data
Can consist of different types of data
Useful for organizing data
Sometimes called a record elsewhere
44
Concept of a StructureConcept of a Structure
john.smithjohn.smith
3535
3.143.14
70.5f70.5f
mm
serviceservice
EmployeeEmployee:name
age
pension
salary
sex
years service
character array
integer
floating point (double)
floating point
character
int
55
Structures in CStructures in C Use the struct keyword
A structure declaration creates a new type
A structure variable definition creates the actual space for a structure
Data items of a structure are called its members
66
Structure DeclarationStructure Declaration
struct employee { char name [ 30 ]; int age; double pension; float salary; char sex; int service;};
a new type!
tag
members
semicolon
77
Structure Variable DefinitionStructure Variable Definition
struct employee staff;
struct employee partTime;
variables of the new type!
tag structure variable names
88
Structure Variable DefinitionStructure Variable Definitionwith Initializerwith Initializer
struct employee newbie = { “john.smith”, 35, 3.14, 70.5f, ‘m’, 0 };
variable with initializer list
99
Structure Declaration andStructure Declaration andVariable DefinitionVariable Definition
struct employee { char name [ 30 ]; int age; double pension; float salary; char sex; int service;} salesDept[100 ];
a new type and a variable definitionNote: this is an array variable declaration
tag
structure variable name
1010
Tag-less Structure Declaration Tag-less Structure Declaration andand
Variable DefinitionVariable Definition
struct { char name [ 30 ]; int age; double pension; float salary; char sex; int service;} newEmployee;
a new tag-less type and a variable definition
No tag
structure variable name
Note: can’t defineanother structurevariable later withan unnamed type
1111
Structure UsageStructure Usage Member access
Allowed operations
Functions
1212
Member AccessMember Access General format:
structVariable.memberName
Examples:
newEmployee.salarysalesDept[22].agesalesDept[i].servicesalesDept[employeeNumber].salary
1313
Structure Member AccessStructure Member Access struct employee { char name [ 30 ]; int age; double pension; float salary; char sex int service;} finDept[100 ];
void print_ages (void) { int i;
for ( i = 0; i < 100; i++) { printf (“Age is %d\n”, finDept[i].age); } }
1414
Allowed OperationsAllowed Operations Can copy or assign as a unit (but not
allowed to compare 2 structures)
Access its members
Use sizeof() operator to obtain size in bytes sizeof (struct employee)
1515
FunctionsFunctions Can pass to/from a function:
a member of a structure an entire structure
Passes a whole structure