18
STRUCTURES

31711 Structures

Embed Size (px)

DESCRIPTION

Structures

Citation preview

Page 1: 31711 Structures

STRUCTURES

Page 2: 31711 Structures

INTRODUCTION

oStructures are collection of related variables under one name

oThe variables can be from many data types

oCommonly used to defined records

oDerived data types which are constructed using intrinsic data types

Intrinsic data types

char, int, long,

float, double

Derived

struct

Page 3: 31711 Structures

STRUCTURES DEFINITIONS struct Typename { StructureMemberDeclarationList; };

struct Student { char name[10]; int id; float test[2]; char grade; };

Tag

Type

Individual member

declarations

Ended with a semicolon

Good programming practice:

Tags begin with capital letters,

variables with small letters!

Page 4: 31711 Structures

STRUCTURES DEFINITION

oKeyword struct introduces structure definition.

oStructure definition does not reserve any memory location

oStructure definition must end with a semicolon.

oMembers can be variables, pointers or arrays of primitive data types, it can also be other structures (nested structure) but not instance of itself

oHowever a member that is a pointer to the same structure is allowable

struct abc{ int member1;

struct abc member2;

};

struct abc{

int member1;

struct abc *member2;

};

Linked list –

ECP1026

Page 5: 31711 Structures

NESTED STRUCTURES

oA structure can have members from different structures, but the members’ structure definition must be defined first

oEx.: Student record that contains record of registration date

StudentRec

name

id

dateReg

day

month

year

test

grade

struct Date

struct Student

struct Date {

int day;

char *month;

int year;

};

struct Student {

char name[10];

int id;

struct Date dateReg;

float test[2];

char grade;

};

Page 6: 31711 Structures

STRUCTURES DECLARATION

oReserve memory

oSimilar to basic data type declaration

struct Student student1;

Variable name

Tag Type

Option 1 Option 2

struct Student {

char name[10];

int id;

float test[2];

char grade;

} student1;

struct {

char name[10];

int id;

float test[2];

char grade;

} student1;

Page 7: 31711 Structures

STRUCTURES DECLARATION

oWe can also have array of structures

Lisa

101

82.5

90

A

Ali

102

76

68.75

B

Thomas

103

55.5

60

C

studentList

name

id

test test[0]

test[1]

grade

Components

struct Student studentList[3];

Page 8: 31711 Structures

STRUCTURES INITIALIZATION

1. With declaration

oThe declaration is done with = and {}

oThe values of members are within the {} and separated by ,

oFor array of structure each record initial values should be grouped within {}

struct Student studentList[3] = {

{"Lisa", 101, {82.5, 90}, 'A'},

{"Ali", 102, {76, 68.75}, 'B'},

{"Thomas", 103, {55.5, 60}, 'C'}

};

2. After declaration

o Using structure member operator/dot operator

struct Student student1 = {"Lisa",101,{82.5,90},'A'};

Page 9: 31711 Structures

ACCESSING MEMBERS OF STRUCTURES

strcpy(student2.name, "Ali"); student2.id = 102; student2.test[0] = 76; student2.test[1] = 68.75; student2.grade = 'B';

The dot operator

Assigned value

Member name

Variable name

Page 10: 31711 Structures

ACCESSING MEMBERS OF STRUCTURES

oAccessing members of array of structures

Use both array notation and the dot operator

Example: Access the name of the student stored in the second element of the array studentList by writing

studentList[1].name

Lisa

101

82.5

90

A

Ali

102

76

68.75

B

Thomas

103

55.5

60

C

studentList

name

id

test test[0]

test[1]

grade

Components

Page 11: 31711 Structures

ACCESSING MEMBERS OF STRUCTURE

oAccessing members of inner nested structure

Use the dot operator twice

Example:

studentRec.dateReg.month

StudentRec

name

id

dateReg

day

month

year

test

grade

struct Date

struct Student

Page 12: 31711 Structures

TYPEDEF

oUsed to create synonyms for previously defined data types.

oUsed in structures for a shorter type name

typedef struct Typename { StructureMemberDeclarationList; } New_Typename;

typedef struct Typename New_Typename;

Page 13: 31711 Structures

POINTERS TO STRUCTURES

oTo access members of structure variables using pointers

oTwo ways of accessing a structure member using a pointer:

1. The structure pointer operator, or the arrow operator (->)

Its syntax: structure_pointer->structure_member

2. The dereference operator (*) + dot operator (.) Its syntax: (*structure_pointer).structure_member

The parentheses is needed as the dot operator has a higher precedence than the pointer dereferencing operator

Page 14: 31711 Structures

POINTERS TO STRUCTURES // Accessing structure variables using pointers

#include <stdio.h>

struct Complex{

double real;

double imag;

};

int main(){

struct Complex c1 = {1.2, 4.5}, c2 = {-3.1, 2.45}, sum;

struct Complex *cptr = &c2;

sum.real = c1.real + cptr->real;

sum.imag = c1.imag + (*cptr).imag;

printf("Sum = %+.2f%+.2fi\n", sum.real, sum.imag);

return 0;

}

Page 15: 31711 Structures

PASSING STRUCTURES TO FUNCTIONS - CALL BY VALUE 1 // Accessing structure variables using pointers

#include <stdio.h>

struct Complex{

double real;

double imag;

};

int main(){

struct Complex c1 = {1.2, 4.5}, c2 = {-3.1, 2.45}, sum;

struct Complex *cptr = &c2;

sum.real = c1.real + cptr->real;

sum.imag = c1.imag + (*cptr).imag;

printf("Sum = %+.2f%+.2fi\n", sum.real, sum.imag);

return 0;

}

void comFunc1(double r ){

r += 10;

printf("In comFunc1: r = %.2f\n", r);

}

void comFunc2(struct Complex com){

com.real += 10;

com.imag += 5;

printf("In comFunc2: com.real = %.2f, "

"com.imag = %.2f\n", com.real, com.imag);

}

Page 16: 31711 Structures

PASSING STRUCTURES TO FUNCTIONS - CALL BY VALUE 2 // Modifying a structure variable through a return #include <stdio.h> struct Complex{ double real; double imag; }; struct Complex comFunc(struct Complex); int main(){ struct Complex c = {1.25, 4.56}; // Assigning the return of the function call to the // variable to be modified c = comFunc(c); printf("c.real = %.2f, c.imag = %.2f", c.real, c.imag); return 0; } struct Complex comFunc2(struct Complex com) { com.real += 10; com.imag += 5; return com; }

Page 17: 31711 Structures

PASSING STRUCTURES TO FUNCTIONS - CALL BY REFERENCE // Modifying structure variables using call-by-reference

#include <stdio.h>

struct Complex{

double real;

double imag;

};

typedef struct Complex Com;

void comSumProd(Com, Com, Com *, Com *);

int main(){

Com c1 = {1.2, 4.5}, c2 = {-3.1, 2.45}, sum, prod;

// Passing values of c1 and c2, and addresses of sum

// and prod to comSumProd

comSumProd(c1, c2, &sum, &prod);

printf("Sum = %.2f%+.2fi\n", sum.real, sum.imag);

printf("Prod = %.2f%+.2fi\n", prod.real, prod.imag);

return 0;

}

void comFunc1(Com c1, Com c2, Com *sptr, Com *pptr ){

(*sptr).real = c1.real + c2.real;

(*sptr).imag = c1.imag + c2.imag;

pptr->real = c1.real * c2.real - c1.imag * c2.imag;

pptr->imag = c1.real * c2.imag + c2.real * c1.imag;

}

Page 18: 31711 Structures

EXERCISE