공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Similar documents
Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Fundamentals of Programming Session 24

Programming for Engineers Structures, Unions

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

typedef int Array[10]; String name; Array ages;

Fundamental of Programming (C)

Chapter 10 C Structures, Unions, Bit Manipulations

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

Kurt Schmidt. October 30, 2018

BSM540 Basics of C Language

OBJECT ORIENTED PROGRAMMING USING C++

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Programming. Structures, enums and unions

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Fundamental of Programming (C)

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Fundamentals of Programming

Character Strings. String-copy Example

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 8 STRUCTURES IN C

CS 61c: Great Ideas in Computer Architecture

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

Principles of C and Memory Management

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C PROGRAMMING Lecture 4. 1st semester

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Lab 3. Pointers Programming Lab (Using C) XU Silei

3.3 Structures. Department of CSE

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

Procedural programming with C

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Structures. Lecture 15 COP 3014 Spring April 16, 2018

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

CSC 215 Structures. Dr. Achraf El Allali

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Structures Unions and Enumerated Datatypes 224

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

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

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

Lecture 3 Tao Wang 1

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

IV Unit Second Part STRUCTURES

Java Primer 1: Types, Classes and Operators

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Variation of Pointers

Lecture 2: C Programming Basic

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

BLM2031 Structured Programming. Zeyneb KURT

C Programming for Engineers Functions

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Programming Review CSC 4320/6320

Example: Pointer Basics

COMP 2355 Introduction to Systems Programming

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Review of the C Programming Language for Principles of Operating Systems

Flow Control. CSC215 Lecture

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

Data Representation and Storage

C: How to Program. Week /Mar/05

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

BSM540 Basics of C Language

STRUCTURES & FILE IO

by Pearson Education, Inc. All Rights Reserved.

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Pointers and Arrays 1

Tin học cơ sở 4$ Structures!

High Performance Computing in C and C++

C Programming Language

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Chapter 2 - Introduction to C Programming

DECLARAING AND INITIALIZING POINTERS

EM108 Software Development for Engineers

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

AMCAT Automata Coding Sample Questions And Answers

Language comparison. C has pointers. Java has references. C++ has pointers and references

Intro to C: Pointers and Arrays

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

More about BOOLEAN issues

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Transcription:

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

STRUCTURE Structures are collections of related variables under one name. Structures may contain variables of many different data types in contrast to arrays that contain only elements of the same data type. ID Number Grade Name struct student { char name[10]; int ID_Number; double Grade; }; Student 1

STRUCTURE DEFINITIONS Structures are derived data types they are constructed using objects of other types. Consider the following structure definition: struct card { char *face; char *suit; }; Keyword struct introduces the structure definition. The identifier card is the structure tag, which names the structure definition and is used with the keyword struct to declare variables of the structure type. 2

STRUCTURE DEFINITIONS In this example, the structure type is struct card. Variables declared within the braces of the structure definition are the structure s members. Members of the same structure type must have unique names, but two different structure types may contain members of the same name without conflict. Each structure definition must end with a semicolon. The definition of struct card contains members face and suit of type char *. Structure members can be variables of the primitive data types (e.g., int, float, etc.), or aggregates, such as arrays and other structures. 3

STRUCTURE DEFINITIONS Structure members can be of many types. struct employee { char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; }; 4

STRUCTURE DEFINITIONS Structure definitions do not reserve any space in memory; rather, each definition creates a new data type that is used to define variables. Structure variables are defined like variables of other types. The definition struct card acard, deck[ 52 ], *cardptr; declares acard to be a variable of type struct card, declares deck to be an array with 52 elements of type struct card and declares cardptr to be a pointer to struct card. 5

STRUCTURE DEFINITIONS Variables of a given structure type may also be declared by placing a comma-separated list of the variable names between the closing brace of the structure definition and the semicolon that ends the structure definition. For example, the preceding definition could have been incorporated into the struct card structure definition as follows: struct card { char *face; char *suit; } acard, deck[ 52 ], *cardptr; The structure tag name is optional. If a structure definition does not contain a structure tag name, variables of the structure type may be declared only in the structure definition not in a separate declaration. 6

STRUCTURE DEFINITIONS The only valid operations that may be performed on structures are: assigning structure variables to structure variables of the same type, taking the address (&) of a structure variable, accessing the members of a structure variable using the sizeof operator to determine the size of a structure variable. Assigning a structure of one type to a strucure of a different type is a compilation error. Structures may not be compared using operators == and!=, because structure members are not necessarily stored in consecutive bytes of memory. 7

INITIALIZING STRUCTURES Structures can be initialized using initializer lists as with arrays. To initialize a structure, follow the variable name in the definition with an equals sign and a brace-enclosed, comma-separated list of initializers. For example, the declaration struct card acard = { "Three", "Hearts" }; creates variable acard to be of type struct card and initializes member face to "Three" and member suit to "Hearts". If there are fewer initializers in the list than members in the structure, the remaining members are automatically initialized to 0, 0.0, or \0 (or NULL if the member is a pointer). Structure variables may also be initialized in assignment statements by assigning a structure variable of the same type, or by assigning values to the individual members of the structure. 8

ACCESSING STRUCTURE MEMBERS Two operators are used to access members of structures: the structure member operator (.) also called the dot operator and the structure pointer operator (->) also called the arrow operator. The structure member operator accesses a structure member via the structure variable name. The structure pointer operator accesses a structure member via a pointer to the structure. struct card acard = { Three, Hearts }; struct card *cardptr = &acard; printf( %s, acard.face ); printf( %s, cardptr->suit); 9

ACCESSING STRUCTURE MEMBERS The expression cardptr->suit is equivalent to (*cardptr).suit, which dereferences the pointer and accesses the member suit using the structure member operator. The parentheses are needed here because the structure member operator (.) has a higher precedence than the pointer dereferencing operator (*). Not using parentheses when referring to a structure members that uses a pointer and the. operator (e.g., *cardptr.suit) is a syntax error. Inserting space between and > components of the structure pointer operator is a syntax error. It is recommended not to put spaces around the -> and. operators. Omitting spaces helps emphasize that expressions the operators are contained in are essentially single variable names. 10

ACCESSING STRUCTURE MEMBERS #include <stdio.h> #include <string.h> struct account { char name[20]; int actnum; double balance; }; structure definition structure variable declaration and initialization void main(void) { struct account mine = {"Hong Gildong", 1001, 300000}; struct account yours; struct account *pyours = &yours; strcpy_s(yours.name, 20, "Kim Cheolsu"); yours.actnum = 1002; yours.balance = 500000; printf("size of struct account = %d\n", sizeof(struct account)); structure pointer string copy printf("%d %d %d\n", &mine.name, &mine.actnum, &mine.balance); printf("%-20s %d %0.2f\n", mine.name, mine.actnum, mine.balance); printf("%-20s %d %0.2f\n", yours.name, pyours->actnum, (*pyours).balance); } accessing structure members 11

STRUCTURE AS A MEMBER OF ANOTHER STRUCTURE #include <stdio.h> struct date{ int year; int month; int day; }; struct account{ struct date opendate; char name[20]; int actnum; double balance; }; void main(void){ a variable opendate of type struct date is used as a member of struct account In order to access the member of opendate, use. operator twice struct account mine = {{2012, 5, 23}, "Hong Gildong", 1001, 300000}; printf("year-month-day: %d-%d-%d\n", mine.opendate.year, mine.opendate.month, mine.opendate.day); printf("%s %d %.2f\n", mine.name, mine.actnum, mine.balance); } 12

TYPEDEF The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. Syntax typedef exisiting-data-type new-type-name1; typedef exisiting-data-type new-type-name1, new-type-name2, ; Example #include <stdio.h> define new data types void main(void){ typedef int year; typedef double rate; year thisyear = 2012; rate currentrate = 5.5; printf("year = %d, Rate = %.1f\n", thisyear, currentrate); } declaring variable using new data types 13

TYPEDEF Names for structure types are often defined with typedef to create shorter type names. For example, the statement typedef struct card Card; defines the new type name Card as a synonym for type struct card. Card can now be used to declare variables of type struct card. Card mycard; 14

TYPEDEF C programmers often use typedef to define a structure type, so a structure tag is not required. For example, the following definition typedef struct { char *face; char *suit; } Card; creates the structure type Card without the need for a separate typedef statement. 15

TYPEDEF #include <stdio.h> typedef struct{ int year; int month; int day; } date; typedef struct{ date opendate; char name[20]; int actnum; double balance; } account; void main(void){ date and account are used as data type without keyword struct account mine = {{2012,5,23}, "Hong Gildong", 1001, 300000}; printf("year-month-day: %d-%d-%d\n", mine.opendate.year, mine.opendate.month, mine.opendate.day); printf("%s %d %.2f\n", mine.name, mine.actnum, mine.balance); } 16

STRUCTURE ARRAYS #include <stdio.h> typedef struct{ char name[25]; int type; int credit; }lecture; char *lectype[] = {"Liberal", "Elective", "Required"}; void main(void){ lecture course2[ ] = {{"Programming Language", 1, 3}, {"Circuit Theory", 2, 3}, {"Korean History", 0, 2}}; int i; int size = sizeof(course2)/sizeof(course2[0]); for (i=0;i<size;i++){ printf("%-25s %-10s %d\n",course2[i].name, lectype[course2[i].type], course2[i].credit); } } 17

USING STRUCTURES WITH FUNCTIONS Structures may be passed to functions by passing individual structure members, by passing an entire structure or by passing a pointer to a structure. When structures or individual structure members are passed to a function, they are passed by value. Therefore, the members of a caller s structure cannot be modified by the called function. To pass a structure by reference, pass the address of the structure variable. Structures may also be used as a return type of functions. 18

USING STRUCTURES WITH FUNCTIONS #include <stdio.h> typedef struct{ double real; double imag; } complex; complex paircomplex1(complex com); void paircomplex2(complex *com); void printcomplex(complex com); void main(void) { complex comp = {3.5, 5.2}, pcomp; printcomplex(comp); pcomp = paircomplex1(comp); printcomplex(pcomp); printcomplex(comp); paircomplex2(&pcomp); printcomplex(pcomp); } void printcomplex(complex com) { printf("complex Number(a+bi) = %5.1f + %5.1fi \n", com.real, com.imag); } complex paircomplex1(complex com) { com.imag = -com.imag; return com; } void paircomplex2(complex *com) { com->imag = -com->imag; } 19