Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

Similar documents
Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

12 CREATING NEW TYPES

CSE 230 Intermediate Programming in C and C++

CS2141 Software Development using C/C++ C++ Basics

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

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

CS349/SE382 A1 C Programming Tutorial

6.096 Introduction to C++ January (IAP) 2009

Structures, Unions, and Typedefs

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 10 C Structures, Unions, Bit Manipulations

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

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

Chapter 8 STRUCTURES IN C

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

Programming for Engineers Structures, Unions

A Fast Review of C Essentials Part I

Types and Structures

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

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

OBJECT ORIENTED PROGRAMMING USING C++

Review Questions II KEY

Chapter-8 DATA TYPES. Introduction. Variable:

Datatypes, Variables, and Operations

C++ Programming Chapter 7 Pointers

... Lecture 12. Pointers

Tokens, Expressions and Control Structures

Data Structures Unit 02

Data types, variables, constants

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Type Checking. Prof. James L. Frankel Harvard University

CS Programming In C

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Basic Types, Variables, Literals, Constants

Programming for Engineers Pointers

Input And Output of C++

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Object-Oriented Programming

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

EL6483: Brief Overview of C Programming Language

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Fundamentals of Programming Session 20

Lecture 05 POINTERS 1

CS201- Introduction to Programming Current Quizzes

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Procedures, Parameters, Values and Variables. Steven R. Bagley

EECS 388 C Introduction. Gary J. Minden August 29, 2016

Lecture10: Structures, Unions and Enumerations 11/26/2012

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

Pointers and Strings Prentice Hall, Inc. All rights reserved.

M.EC201 Programming language

Presented By : Gaurav Juneja

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Inductive Data Types

CS201 - Introduction to Programming Glossary By

Data Representation and Storage. Some definitions (in C)

Fundamentals of Programming Session 24

Technical Questions. Q 1) What are the key features in C programming language?

Basic Types & User Defined Types

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Character Strings. String-copy Example

IV Unit Second Part STRUCTURES

Computational Expression

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Computer Systems Principles. C Pointers

Enumerated Types. Mr. Dave Clausen La Cañada High School

Structures and Pointers

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

Programming in C and C++

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

BLM2031 Structured Programming. Zeyneb KURT

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

More about BOOLEAN issues

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

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Pointer Declarations

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

A flow chart is a graphical or symbolic representation of a process.

Programming, numerics and optimization

High Performance Computing

User Defined data Types

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Pointers, Dynamic Data, and Reference Types

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

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

Transcription:

Today s Goals Section 1 Structures Structures Types and variables typedef structs and pointers Unions Enumerations To group multiple (heterogeneous) variables Similar to Java classes, but not as powerful A structure has only data members All members are public CS246 1 Lec12 CS246 2 Lec12 Structure Operations Structure Type Declaration Structure type declaration Structure variable declaration Member assignment/reference Structure initialization Structure assignment struct StructType { /* members */ ; Typically global Members Analogous to data declaration struct Aircraft{ ; CS246 3 Lec12 CS246 4 Lec12 Aircraft identifies a structure type, also known as a structure tag. a is an instance of the structure type Aircraft Keyword struct may not be dropped Struct Instance struct Aircraft { / *members*/ ; structure tag struct Aircraft a; CS246 5 Lec12 typedef A way to define a synonym for existing (complicated) types. typedef int Bool; typedef int*** Intptr3; typedefed type names by convention have the first letter in uppercase. Besides programmer laziness, typedef does contributes to portability (size_t) typedef long Myint; others typedef int Myint; machines with 32-bit int CS246 6 Lec12 1

typedef and Structures Structure Variable Declaration This is a case of programmer laziness! Instead of struct Aircraft boeing747; use typedef struct Aircraft Arcrft; then Arcrft boeing747; Arcrft is a new user-defined type. struct Ac{ a, b; struct Ac c; typedef struct Ac{ Aircraft; Aircraft a, b, c; CS246 7 Lec12 CS246 8 Lec12 Member Assignment/Reference Structure Initialization Assignment pattern structvar.membername = exp; Reference pattern structvar.membername Aircraft; Aircraft a; a.z = 0; a.prevz = a.z; Like array initializations, this only works at the time of declaration. Afterwards you must assign/initialize each member one by one. Aircraft; Aircraft a = {"N3NK", 0, 0, 0, 0, 270, 0, 0; CS246 9 Lec12 CS246 10 Lec12 Structure Assignment Additional Examples structvar1 = structvar2; Each member s value will be copied Aircraft; Aircraft a = {"N3NK", 0, 0, 0, 0, 270, 0, 0; Aircraft b; b = a; int ssn; float debt; Person; int type; int value; int address; char name[32]; Variable; ssn debt type value address name CS246 11 Lec12 CS246 12 Lec12 2

Section 2 Complex Structures Another Example Various structure members Basic types: int, double, char, etc. Arrays Pointers Structures Arbitrary combination possible Position; Aircraft; Position northeast_corner; Position southwest_corner; int height; Mountain; CS246 13 Lec12 CS246 14 Lec12 Aircraft; Array of Structures Aircraft aircrafts[2] = { { init list for elem 0, { init list for elem 1 ; aircrafts[0].pos.x = 0; CS246 15 Lec12 Structure with Array of Structures Aircraft; int numofaircrafts; Aircraft aircrafts[100]; Radar; Radar r; r.aircrafts[0].pos.x = 0; CS246 16 Lec12 Section 3 Structure Arguments Section 2 Structure Return void updatestatus(aircraft b) { b.heading += 90; updatestatus(a); The argument variable b is a copy of the original variable a. Analogous to basic variables, different from arrays Cannot change the original variable a CS246 17 Lec12 Aircraft updatestatus(aircraft b) { b.heading += 90; return b; a = updatestatus(a); The local variable b is modified and returned. The returned b can be assigned (copied) to the original a. CS246 18 Lec12 3

Section 4 Pointer to Structure To modify the original value, pass the pointer to a structure void updatestatus(aircraft *b) { (*b).heading += 90; updatestatus(&a); CS246 19 Lec12 Shorthand To deal with pointers to structure, the shorthand form is more commonly used. StructPtrVar member-of-structure; void updatestatus(aircraft *b) { b->heading += 90; /* same as (*b).heading */ updatestatus(&a); CS246 20 Lec12 Section 5 Unions A union, like a structure, consists of data members. The compiler will only allocate enough space for the largest member in a union. All member of a union overlay each other (i.e. they are stored in the same address). struct { s; i f s union { u; CS246 21 Lec12 i u f Mixed types Tag field Unions Usages typedef union{ Number; int type; union{ u; Number; Number a[100]; a[0].i = 5; a[1].f = 5.5; void print(number n){ switch(n.type) { case(integer): printf("%d", n.u.i); case(float): printf("%f", n.u.f); CS246 22 Lec12 Section 6 Enumerations A special type in C whose values are enumerated by the programmer A way to group a set of related #defines. #define SUIT int #define CLUB 0 #define DIAMOND 1 #define HEART 2 #define SPADE 3 typedef enum {FALSE, TRUE Bool; enum {CLUB, DIAMOND, HEART, SPADE; enum SUIT {CLUB, DIAMOND, HEART, SPADE; SUIT s1 = HEART, s2; typedef enum {CLUB,DIAMOND,HEART,SPADE Suit; If unspecified, enums by default start from 0 and increment by 1 CS246 23 Lec12 Enumerations as Integers All enums are integers. More flexible enum Specify values: enum REDSUIT {HEART=10, DIAMOND=1; If no value specified, value is 1 greater than the previous constant (first constant is by default 0): enum EGA {BLACK,LTGRAY=7,DKGRAY,WHITE=15; C allows mixing enum and int enum {CLUB,DIAMOND,HEART,SPADE s; int i = DIAMOND; // i is 1 s = 2; // s is HEART i++; // i is HEART CS246 24 Lec12 4

Summary structs are much like Java s classes. Use union with care. Learn how to incorporate enum into your programming. enums are thinly disguised ints, and the C compiler allows mixing. CS246 25 Lec12 5