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

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

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

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 Pointers (1A) Young Won Lim 12/26/17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 1. Young Won Lim 11/17/17

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

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

Example 2. Young Won Lim 11/24/17

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Monad (1A) Young Won Lim 6/9/17

Functions (4A) Young Won Lim 5/8/17

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

Structure (1A) Component

Monad Background (3A) Young Won Lim 11/20/17

Haskell Overview II (2A) Young Won Lim 8/9/16

Functions (4A) Young Won Lim 3/16/18

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

RAM (1A) Young Won Lim 11/12/13

GHCi: Getting started (1A) Young Won Lim 5/26/17

Algorithms (7A) Young Won Lim 4/10/17

Side Effects (3A) Young Won Lim 1/13/18

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Algorithms (7A) Young Won Lim 4/18/17

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

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Haskell Overview III (3A) Young Won Lim 10/4/16

Background Constructors (1A) Young Won Lim 1/22/18

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

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

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

Background Constructors (1A) Young Won Lim 7/5/18

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

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

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

CORDIC Fixed Point Simulation. Young Won Lim 2/28/12

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

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

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

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

Background Functions (1C) Young Won Lim 4/3/18

Tiny CPU Data Path (2B) Young Won Lim 5/2/16

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

CSC231 C Tutorial Fall 2018 Introduction to C

Shared Memory (8A) Shared Memory

Monad (1A) Young Won Lim 6/21/17

Character Strings. String-copy Example

Background Constructors (1A) Young Won Lim 6/30/18

Transcription:

Structure (1A)

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

Struct Declaration (1) definition } var ; struct aaa var; var declaration int add (int x, int y) ; prototype address data int add (int x, int y) { return (x + y); } definition &var = &var.i &var.s &var.c var.i var.s var.c sum = add (30, 20) ; function call Structure 3

Struct Declaration (2) typedef } ATYPE ; typedef struct aaa ATYPE ; ATYPE var; ATYPE var; struct aaa var; Structure 4

Struct Declaration (3) typedef struct aaa ATYPE ; ATYPE var; struct aaa var; } var ; typedef } ATYPE ; ATYPE var; Structure 5

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 Structure 6

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 Structure 7

Initialization and Assignment definition address data &v1.s v1.s &v1.i v1.i &v1.c v1.c struct aaa v1 = { 100, 10, 'A' struct aaa v2; v2 = v1 ; v2.i = v1.i ; v2.s = v1.s ; v2.c = v1.c ; &v2.s &v2.i &v2.c v2.s v2.i v2.c Structure 8

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. ); } Structure 9

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.name[i] ); printf( stid: %d \n, S.stid[i] ); printf( eng: %d \n, S.eng[i] ); printf( math: %d \n, S.math[i] ); printf( gpa: %f \n, (S.eng[i] + S.math[i]) / 2. ); } Structure 10

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 ); Structure 11

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 ); Structure 12

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 Structure 13

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