Structures (1A) Young Won Lim 12/4/17

Similar documents
Structures (1A) Young Won Lim 11/8/16

Structure (1A) Young Won Lim 7/30/13

Applications of Structures (1A) Young Won Lim 12/8/17

Applications of Structures (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 11/1/17

Applications of Arrays (1A) Young Won Lim 2/11/17

Applications of Pointers (1A) Young Won Lim 12/26/17

Applications of Arrays (1A) Young Won Lim 3/15/17

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 3/5/18

Arrays (1A) Young Won Lim 1/27/17

Arrays (1A) Young Won Lim 12/4/17

Function Overview (1A) Young Won Lim 10/23/17

Overview (1A) Young Won Lim 9/25/17

Applications of Pointers (1A) Young Won Lim 2/27/18

Overview (1A) Young Won Lim 9/14/17

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 4/11/18

Overview (1A) Young Won Lim 9/9/17

Applications of Pointers (1A) Young Won Lim 3/21/18

Applications of Pointers (1A) Young Won Lim 3/31/18

Pointers (1A) Young Won Lim 1/14/18

Pointers (1A) Young Won Lim 1/22/18

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 2/10/18

Applications of Pointers (1A) Young Won Lim 4/24/18

Applications of Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 10/18/17

Pointers (1A) Young Won Lim 10/23/17

Pointers (1A) Young Won Lim 12/4/17

Type (1A) Young Won Lim 2/17/18

File (1A) Young Won Lim 11/25/16

Example 3 : using a structure array. Young Won Lim 11/25/17

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Arrays and Strings (2H) Young Won Lim 3/7/18

Example 1. Young Won Lim 11/17/17

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Example 2. Young Won Lim 11/24/17

Example 3. Young Won Lim 11/22/17

Memory (1A) Young Won Lim 9/7/17

Number System (1A) Young Won Lim 7/7/10

Applications of Array Pointers (1A) Young Won Lim 11/22/18

Expressions (2E) Young Won Lim 4/9/18

Variables (2D) Young Won Lim 3/28/18

Pointers (2G) Young Won Lim 3/7/18

Expressions (2E) Young Won Lim 3/10/18

ELF (1A) Young Won Lim 10/22/14

Program Structure (2A) Young Won Lim 3/8/18

Program Structure (2A) Young Won Lim 5/28/18

Accessibility (1A) Young Won Lim 8/22/13

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Class (1A) Young Won Lim 9/8/14

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Structures (2I) Young Won Lim 4/17/18

Structures (2I) Young Won Lim 3/7/18

Computer Systems Principles. C Pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Class (1A) Young Won Lim 11/20/14

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

ELF (1A) Young Won Lim 3/24/16

Methods (2A) Young Won Lim 12/10/14

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

Binary Search Tree (3A) Young Won Lim 6/2/18

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CS 261 Data Structures. Introduction to C Programming

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Binary Search Tree (2A) Young Won Lim 5/17/18

Character Strings. String-copy Example

C++ Structures Programming Workshop 2 (CSCI 1061U)

Polymorphism (1A) Young Won Lim 8/22/13

Methods (2A) Young Won Lim 10/14/14

Low-Level C Programming. Memory map Pointers Arrays Structures

Polymorphism (1A) Young Won Lim 8/15/13

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Haskell Overview IV (4A) Young Won Lim 10/13/16

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Polymorphism Overview (1A) Young Won Lim 2/20/18

Pointers and Arrays 1

IV Unit Second Part STRUCTURES

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

POINTER & REFERENCE VARIABLES

Eulerian Cycle (2A) Young Won Lim 4/26/18

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Example : Define a structure of student having fields Roll No, Name, Address and fees paid ( Make appropriate assumption about datatypes) Method -1

CSC231 C Tutorial Fall 2018 Introduction to C

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Preprocessing (2K) Young Won Lim 3/7/18

Data Representation and Storage. Some definitions (in C)

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

First of all, it is a variable, just like other variables you studied

Binary Search Tree (3A) Young Won Lim 6/4/18

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

State Monad (3D) Young Won Lim 9/25/17

Transcription:

Structures (1A)

Copyright (c) 2010-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.

Structure Variable Declaration accessing a structure variable definition &var var struct aaa var; var declaration &var = &var.i &var.s &var.c var.i var.s var.c accessing its members 3

Compare with function definitions function type int add (int x, int y) ; prototype definition int add (int x, int y) { return (x + y); } definition struct aaa var; var declaration var.i = 100; member access sum = add (30, 20) ; function call 4

Struct Variable Declaration (1) definition } var ; struct aaa var; var declaration 5

Struct Variable Declaration (2) definition } var ; struct aaa var; var declaration definition + variable declaration 6

Struct Variable Declaration (3) typedef } ATYPE ; typedef struct aaa ATYPE ; ATYPE var; ATYPE var; 7

Struct Variable Declaration (4) typedef } ATYPE ; typedef struct aaa ATYPE ; ATYPE var; ATYPE var; 8

Struct Variable Declaration Summary struct aaa var; typedef struct aaa ATYPE ; ATYPE var; } var ; typedef } ATYPE ; ATYPE var; 9

Initialization &v1.s &v1.i &v1.c v1.s v1.i v1.c =100 =10 = A &v2.s &v2.i &v2.c v2.s v2.i v2.c struct aaa v1 = { 100, 10, 'A' struct aaa v2; 10

Assignments &v1.s &v1.i &v1.c v1.s v1.i v1.c =100 =10 = A &v2.s &v2.i &v2.c v2.s v2.i v2.c v2 = v1 ; v1 v2 v2.i = v1.i ; v2.s = v1.s ; v2.c = v1.c ; v1.s v1.i v1.c =100 =10 = A v2.s v2.i v2.c =100 =10 = A 11

Variable Declaration Allocates Memory definition struct aaa v1 = { 100, 10, 'A' &v1 v1.s =100 v1.i =10 v1.c = A 12

Pointer to Structure Variable Declaration p struct aaa *p ; &v1 v1.s =100 v1.i =10 v1.c = A pointer variable assignment p p = &v1; &v1 p->s p->i p->c 13

Dereferencing Pointers to Structures (1) (*p).mem p->mem &var var &var = &var.i var.i &var.s var.s &var.c var.c p p (*p) (*p)->s (*p)->i (*p)->c = p->s = p->i = p->c = v1.s = v1.i = v1.c 14

Dereferencing Pointers to Structures (2) *p.mem *(p.mem) p : a struct variable mem : another pointer variable &p p &p = &p.i p.i &p.s p.s &p.q p.q *p.q 15

Dereferencing Pointers to Structures (3) *p->mem *(p->mem) p : a pointer to a struct variable mem: another pointer variable p p (*p) (*p)->s (*p)->i (*p)->q = p->s = p->i = p->q = v1.s = v1.i = v1.q *p->q 16

Dereferencing Pointers to Structures definition (*p).mem p->mem struct aaa v1 = { 100, 10, 'A' *p.mem *(p.mem) struct aaa *p = &v1; *p->mem *(p->mem) 17

Memory Layout (1) definition sizeof (int) = 4 bytes sizeof (short) = 2 bytes sizeof (char) = 1 bytes &var.i (int) var.i struct aaa var; var declaration &var.s (short) var.s address data &var.c (char) var.i &var.i &var.s &var.c var.i var.s var.c sizeof (var) = 8 bytes 18

Memory Layout (2) definition &var.s (short) var.s struct aaa var; var declaration &var.i (int) var.i address data &var.c (char) var.i &var.s &var.i &var.c var.s var.i var.c sizeof (var) = 12 bytes 19

Struct Definition and Declarations definition 8 bits var 8 bits struct aaa var; var declaration var.i var.c struct aaa *ptr; var declaration ptr 20

Nested Structures struct point { int x ; int y ; struct rect { struct point p1 ; struct point p2 ; address data &r.p1 r.p1 &r.p2 r.p2 struct point pa ; struct rect r ; struct point pb ; &r.p1.x r.p1.x &r.p1.y r.p1.y pa.x = 10 ; pa.y = 20 ; pb.x = 300 ; pb.y = 400 ; r.p1 = pa ; r.p2 = pb ; &r.p2.x &r.p2.y r.p2.x r.p2.y 21

Array Implementation // within main() int i ; char * name[3] = { John, Mary, Baker }; int stid[3] = { 201101, 201102, 201103 }; int eng[3] = { 94, 85, 90 }; int math[3] = { 88, 92, 98 }; double gpa[3]; for (i=0; i<3; ++i) printf( -----------------------------\n ); printf( name: %s \n, name[i] ); printf( stid: %d \n, stid[i] ); printf( eng: %d \n, eng[i] ); printf( math: %d \n, math[i] ); printf( gpa: %f \n, (eng[i] + math[i]) / 2. ); } 22

Struct Implementation // outside main() struct srec { char * name ; int stid ; int eng ; int math ; double gpa ; // within main() struct srec S[3] = { { John, 201101, 94, 88 }, { Mary, 201102, 85, 92 }, { Baker, 201103, 90, 98 } }; for (i=0; i<3; ++i) printf( -----------------------------\n ); printf( name: %s \n, S[i].name ); printf( stid: %d \n, S[i].stid ); printf( eng: %d \n, S[i].eng ); printf( math: %d \n, S[i].math ); printf( gpa: %f \n, (S[i].eng + S[i].math) / 2. ); } 23

Struct as a function argument // outside main() struct srec { char * name ; int stid ; int eng ; int math ; double gpa ; double compute_gpa (struct srec R) { return (R.eng + R.math) / 2. ; } // within main() struct srec S = { John, 201101, 94, 88 }; gpa = compute_gpa ( S ); double compute_gpa (struct srec R) gpa = compute_gpa ( S ); S.name, S.stid, S.eng, S.math, S.gpa char *, int, int, int, double R.name, R.stid, R.eng, R.math, R.gpa double compute_gpa (struct srec R) double gpa; gpa = compute_gpa ( S ); 24

Struct returning function // outside main() struct srec { char * name ; int stid ; int eng ; int math ; double gpa ; struct srec calc_gpa (struct srec R) { R.gpa = (R.eng + R.math) / 2. ; return R; } // within main() struct srec S = { John, 201101, 94, 88 }; gpa = calc_gpa ( S ); struct srec calc_gpa (struct srec R) S = calc_gpa ( S ); S.name, S.stid, S.eng, S.math, S.gpa char *, int, int, int, double R.name, R.stid, R.eng, R.math, R.gpa struct srec calc_gpa (struct srec R) double gpa; S = calc_gpa ( S ); 25

Struct pointer as a function argument // outside main() struct srec { char * name ; int stid ; int eng ; int math ; double gpa ; void fill_gpa (struct srec * R) { (*R).gpa = ((*R).eng + (*R).math) / 2. ; } // within main() struct srec S = { John, 201101, 94, 88 }; &S R gpa = fill_gpa ( &S ); void fill_gpa (struct srec * R) ; S.name, S.stid, S.eng, S.math, S.gpa gpa = fill_gpa ( &S ); void fill_gpa (struct srec * R) ; double gpa; gpa = fill_gpa ( &S ); (*R).eng (*R).math (*R).gpa R->eng R->math R->gpa 26

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 27