Applications of Structures (1A) · typedef struct aaa {int data; struct aaa *next;} ATYPE ; typedef...

Post on 12-Jul-2020

23 views 0 download

Transcript of Applications of Structures (1A) · typedef struct aaa {int data; struct aaa *next;} ATYPE ; typedef...

Young Won Lim4/23/19

Applications of Structures (1A)

Young Won Lim4/23/19

Copyright (c) 2009 - 2017 Young W. Lim.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

Please send corrections (or suggestions) to youngwlim@hotmail.com.

This document was produced by using LibreOffice.

Series: 6Applications of Structures

3 Young Won Lim4/23/19

An Array of StructuresIncomplete Definition of StructuresSelf-Referential Structures

Series: 6Applications of Structures

4 Young Won Lim4/23/19

Defining an array of a structure

Student ID Korean English Math Average

S[0]S[1]S[2]S[3]S[4]S[5]S[6]S[7]

S[i].I S[i].K S[i].E S[i].M S[i].A

struct Stype { int I; int K; int E; int M; double A;};

struct Stype S[SIZE];

Series: 6Applications of Structures

5 Young Won Lim4/23/19

S[i].I, S[i].K, S[i].E, S[i].M, S[i].A

Student ID Korean English Math Average

S[i]

S[i].I S[i].K S[i].E S[i].M S[i].A

struct Stype { int I; int K; int E; int M; double A;};

struct Stype S[SIZE];

S[i].I ((S[i]).I)

Series: 6Applications of Structures

6 Young Won Lim4/23/19

Accessing members of an array of a structure

struct Stype { int I; int K; int E; int M; double A;};

struct Stype S[SIZE];

[ ] , . > *

S[i].I ((S[i]).I)

(*(S+i)).I

(S+i)->I

*(S+i).I *((S+i).I)

Series: 6Applications of Structures

7 Young Won Lim4/23/19

Pointer to a Structure Array

Student ID Korean English Math Average

S[0]S[1]S[2]S[3]S[4]S[5]S[6]S[7]

S[i].I S[i].K S[i].E S[i].M S[i].A

S

struct Stype { int I; int K; int E; int M; double A;};

struct Stype S[SIZE];

struct Stype *T = &S[5];

T

Series: 6Applications of Structures

8 Young Won Lim4/23/19

Passing Arrays of Structures

void func (struct Stype T[]);

S Array Name

Starting Address

T

struct Stype { int I; int K; int E; int M; double A;};

struct Stype S[SIZE];

Pass by Reference

Series: 6Applications of Structures

9 Young Won Lim4/23/19

Defining an array of a structure

S.K[0]S.K[1]S.K[2]S.K[3]S.K[4]S.K[5]S.K[6]S.K[7]

Sstruct Stype { int K[SIZE];};

struct Stype S;

void func( struct Stype S )

Can pass the entire array contents by value

Series: 6Applications of Structures

10 Young Won Lim4/23/19

Accessing array members

struct Stype { int K[SIZE];};

struct Stype S[SIZE];

[ ] , . > * S.K[i] ((S.K)[i])

*(S.K+i)

S.(*(K+i))

S.*(K+i)

S.(K+i)

Series: 6Applications of Structures

11 Young Won Lim4/23/19

Struct Variable Declaration Summary

struct aaa {

int i;

short s;

char c;

} ;

struct aaa {

int i;

short s;

char c;

} ;

struct aaa {

int i;

short s;

char c;

} var ;

struct aaa var; ATYPE var;

ATYPE var;

typedef struct aaa {

int i;

short s;

char c;

} ATYPE ;

typedef struct aaa ATYPE ;

Series: 6Applications of Structures

12 Young Won Lim4/23/19

Incomplete Definition Range

struct aaa { int i; short s; char c; };

At all these points, the structuretype definition is incomplete

From here, the definition complete.

incomplete definition can be used in cases where the exact size information is not required

Dereferencing requires size informationMember assignment require size informationLocal variable declaration require size informationLocal pointer variable takes always 8bytes on a 64-bit machines

Global variable

Series: 6Applications of Structures

13 Young Won Lim4/23/19

Incomplete Definition Usages

struct bbb { int i; struct bbb *s; };incomplete definition can be used with pointer variable declaration

typedef struct ccc Ctype;incomplete definition can be used with typedef

void func( struct ddd *Ctype ); incomplete definition can be used with pointer parameter declaration

Series: 6Applications of Structures

14 Young Won Lim4/23/19

Self-Referential Structures

struct bbb { int i; struct bbb *s; };incomplete definition can be used with pointer variable declaration

Series: 6Applications of Structures

15 Young Won Lim4/23/19

Self-Referential Structure

struct aaa {

int data;

struct aaa * next;

} ;incomplete definition points to the self type

Series: 6Applications of Structures

16 Young Won Lim4/23/19

Self-Referential Structure Definitions

struct aaa {

int data;

struct aaa *next;

} ;

structure type

struct aaa {

int data;

struct aaa *next;

} ;

struct aaa {

int data;

struct aaa *next;

} var ;

struct aaa var; ATYPE var;

structure type

structure type

ATYPE var;

structure type

typedef struct aaa {

int data;

struct aaa *next;

} ATYPE ;

typedef struct aaa ATYPE ;

Series: 6Applications of Structures

17 Young Won Lim4/23/19

Changing Orders : Global Struct Variable Declaration

struct aaa {

int data;

struct aaa *next;

} ;

struct aaa {

int data;

struct aaa *next;

} ;

struct aaa {

int data;

struct aaa *next;

} var ;

struct aaa var; ATYPE var;

ATYPE var;

typedef struct aaa {

int data;

struct aaa *next;

} ATYPE ;

typedef struct aaa ATYPE ;

Global Structure Variable Declaration is possible before the completestructure definition.

Local Structure Variable Declarationis not possible

Series: 6Applications of Structures

18 Young Won Lim4/23/19

Changing Tag Names

struct Atype {

int data;

struct Atype *next;

} ;

struct Atype {

int data;

struct Atype *next;

} ;

struct Atype {

int data;

struct Atype *next;

} var ;

struct Atype var; Atype var;

Atype var;

typedef struct Atype {

int data;

struct Atype *next;

} Atype ;

typedef struct Atype Atype ;

aaa Atype : tag name

Atype : new type

global variable : var

Series: 6Applications of Structures

19 Young Won Lim4/23/19

Self-Referential Data Structures

struct node {

int data;

struct node * next;

} ;

typedef struct node node ;

struct node {

int data;

struct node * next;

} ;

typedef struct node node ;

struct node {

int data;

node * next;

} ;

typedef struct node node ;

struct node {

int data;

node * next;

} ;

typedef struct node node ;

Series: 6Applications of Structures

20 Young Won Lim4/23/19

Comparison with a structure type

int x[4];

A 1-d array

x[0]x[1]x[2]x[3]

x

x.a

x.bx.c

x

x.d

struct aaa {long a;int b;long c;int d;

} x;

Series: 6Applications of Structures

21 Young Won Lim4/23/19

References

[1] Essential C, Nick Parlante[2] Efficient C Programming, Mark A. Weiss[3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr.[4] C Language Express, I. K. Chun[5] https://pdos.csail.mit.edu/6.828/2008/readings/pointers.pdf