1 c language structures. 2 topics concept of a structure concept of a structure structures in c...

15
1 C Language C Language Structures Structures

Upload: valentine-johnson

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

11

C LanguageC Language

StructuresStructures

Page 2: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 3: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 4: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 5: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 6: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 7: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

77

Structure Variable DefinitionStructure Variable Definition

struct employee staff;

struct employee partTime;

variables of the new type!

tag structure variable names

Page 8: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

88

Structure Variable DefinitionStructure Variable Definitionwith Initializerwith Initializer

struct employee newbie = { “john.smith”, 35, 3.14, 70.5f, ‘m’, 0 };

variable with initializer list

Page 9: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 10: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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

Page 11: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

1111

Structure UsageStructure Usage Member access

Allowed operations

Functions

Page 12: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

1212

Member AccessMember Access General format:

structVariable.memberName

Examples:

newEmployee.salarysalesDept[22].agesalesDept[i].servicesalesDept[employeeNumber].salary

Page 13: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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); } }

Page 14: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

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)

Page 15: 1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration

1515

FunctionsFunctions Can pass to/from a function:

a member of a structure an entire structure

Passes a whole structure