Deriving Types II COMP 1002/1402 Nesting Structures So far, only - - PDF document

deriving types ii
SMART_READER_LITE
LIVE PREVIEW

Deriving Types II COMP 1002/1402 Nesting Structures So far, only - - PDF document

Deriving Types II COMP 1002/1402 Nesting Structures So far, only basic types appeared in struct Question : Why not defined types? Answer : No reason. Simplicity of presentation 1 Nesting Structures Define a STAMP to contain: a DATE and a


slide-1
SLIDE 1

1

Deriving Types II

COMP 1002/1402

Nesting Structures

So far, only basic types appeared in struct Question : Why not defined types? Answer : No reason. Simplicity of presentation

slide-2
SLIDE 2

2

Nesting Structures

Define a STAMP to contain: a DATE and a TIME

How to do it

Use one struct or type inside the overall struct See following examples... Style: Declare every structure separately! Necessity: Declare before use.

slide-3
SLIDE 3

3

Bad Style

typedef struct { struct { int month; int day; int year; } date; struct { int hour; int min; int sec; } time; } STAMP; STAMP aStamp;

Good Style

typedef struct { int month; int day; int year; } DATE; typedef struct { int hour; int min; int sec; } TIME; typedef struct { DATE date; TIME time; } STAMP; STAMP aStamp;

slide-4
SLIDE 4

4

Referencing Nested Structures

aStamp aStamp.date aStamp.date.month aStamp.date.day aStamp.date.year aStamp.time aStamp.time.hour aStamp.time.min aStamp.time.sec

Nested Structure Initialization

Initialize each structure with: Nested {} :

STAMP aStamp = {{05,10,1936},{23,45,00}};

Or predefined variables:

DATE aDate = {05,10,1936}; TIME aTime = {23,45,00}; STAMP aStamp = {aDate, aTime};

slide-5
SLIDE 5

5

Arrays in Structures

Defined like any other element Accessed through indices Initialized like a nested (sub)structure

Arrays in Structures

slide-6
SLIDE 6

6

Accessing Elements

STUDENT aStudent; aStudent aStudent.name aStudent.name[1] aStudent.midterm aStudent.midterm[j] aStudent.final

Accessing Elements

STUDENT *paStudent; paStudent = &aStudent; paStudent->name paStudent->name[1] paStudent->midterm paStudent->midterm[j] paStudent->final

slide-7
SLIDE 7

7

Accessing pointers

STUDENT aStudent={"John Smith",{92,80,70},87}; int *pScores = aStudent.midterm; int totalScores = *pScores + *(pScores+1) + *(pScores+2);

Pointers, Structures and Memory

Consider the DATE structure Months should be strings! Should we store the string in every month? Store one pointer in every structure

slide-8
SLIDE 8

8

The New Structure

typedef struct { char *month; int day; int year; } DATE;

The New Structure

Every "December" points to the same spot!

slide-9
SLIDE 9

9

Array of Structures

STUDENT stuAry[50];

Array of Structures

int totScore = 0; float average; STUDENT *pStu; STUDENT *pLastStu; … pLastStu = stuAry + 49; /*Address of Last one*/ for (pStu = stuAry; pStu <= pLastAry; pStu++) totScore += pStu->final; average = totScore / 50.0;

slide-10
SLIDE 10

10

Structures and Functions

  • 1. Pass individual members (fields)
  • 2. Pass entire structure : BY VALUE
  • 3. Pass address to structure

Passing Individual Members

slide-11
SLIDE 11

11

Sending the Whole Structure Careful Passing by Value !

Any pointers in the structure ? What about that new DATE class… Pass a DATE to a function by value, In the function change the contents of month, What happens?

slide-12
SLIDE 12

12

Passing Whole Structures Unions

union like enum & struct allow:

  • 1. A single variable
  • 2. Multiple variables
  • 3. New type definition
slide-13
SLIDE 13

13

Union

A union variable allows: more than one type of data to occupy its memory! Big enough for the largest of them. Accessed with . Operator (See example - Next page) (data.num or data.chAry[0])

Union

slide-14
SLIDE 14

14

Unions in Structures Initializing Unions

Only the first type declared in union! typedef union { short num; char ch[2]; } SH_CH2; SH_CH2 data = 16706; printf("%d\n%c\n%c\n",num,ch[0],ch[1]);

slide-15
SLIDE 15

15

Little Endian, Big Endian

  • Q. What does this code produce?

SH_CH2 data = 16706; printf("%d\n%c\n%c\n",num,ch[0],ch[1]);

  • Ans. Two possible outputs!

16706 16706 A B B A

Why?

(16706)10 = (0100 0001 0100 0010)2

Big Endian puts msw before lsw Little Endian puts lsw before msw (Most Significant Word & Least Significant Word) Word is 2 bytes (usually)!