Principles of Programming Languages Topic: Imperative Programming II

Similar documents
Principles of Programming Languages Topic: Imperative Programming III Professor Lou Steinberg Fall 2004

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers (part 1)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers (part 1)

Lectures 5-6: Introduction to C

Character Strings. String-copy Example

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Lectures 5-6: Introduction to C

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

CS61C : Machine Structures

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

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; };

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

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

CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Dynamic Data Structures. CSCI 112: Programming in C

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

ECE 15B COMPUTER ORGANIZATION

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

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:

Subject: Fundamental of Computer Programming 2068

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

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

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Array Initialization

Approximately a Final Exam CPSC 206

Data Type Fall 2014 Jinkyu Jeong

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Algorithms & Data Structures

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Kurt Schmidt. October 30, 2018

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Arrays, Pointers and Memory Management

CS 61C: Great Ideas in Computer Architecture Introduction to C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Arrays and Pointers (part 2) Be extra careful with pointers!

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

IV Unit Second Part STRUCTURES

A Fast Review of C Essentials Part I

Arrays and Pointers (part 2) Be extra careful with pointers!

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

BLM2031 Structured Programming. Zeyneb KURT

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS 61c: Great Ideas in Computer Architecture

C PROGRAMMING Lecture 4. 1st semester

Computer Programming: Skills & Concepts (CP) Strings

Content. In this chapter, you will learn:

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Data Representation and Storage. Some definitions (in C)

Class Information ANNOUCEMENTS

CSC231 C Tutorial Fall 2018 Introduction to C

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

CSC 2400: Computer Systems. Arrays and Strings in C

CGS 3460 Summer 07 Midterm Exam

EM108 Software Development for Engineers

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

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

ENEE150 Final Exam Review

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

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Chapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Heap Arrays. Steven R. Bagley

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

CS13002 Programming and Data Structures, Spring 2005

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Chapter 2: Overview of C. Problem Solving & Program Design in C

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring 2015

C Programming Review CSC 4320/6320

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Multidimension array, array of strings

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

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

Compiling and Running a C Program in Unix

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C programming basics T3-1 -

Introduction to C. Systems Programming Concepts

CS349/SE382 A1 C Programming Tutorial

CSE 124 Discussion (10/3) C/C++ Basics

ANSI C Reserved Words

Transcription:

Principles of Programming Languages Topic: Imperative Programming II Imperative Programming 1

Employee.c #include<stdio.h> int find_over49(); void *malloc(); /*this makes these definitions and variables globals*/ typedef struct cell employee; struct cell{ int age; char *name; employee *next; ; employee *company[2];/*defines an array of pointers to employee's*/ /*each element of this array points to a struct cell*/ Imperative Programming 4

Employee.c, cont. void main(void) { employee *z; /* create storage for employee records*/ for (i=0; i<2; i++) company[i] = (employee *)malloc(sizeof(employee)); /* initialize company array */ (company[1])->age = 55; (company[1])->name = Barbara Ryder"; (company[0])->age = 29; (company[0])->name = "Beth Ryder"; if (find_over49(&z)!= 0) printf(" %s \n",z->name); Imperative Programming 5

Example /* z is of type employee *, so address(z) is value of an employee ** var */ int find_over49(employee **a) { int ans, i; ans = -1; for (i=0; i<2; i++) if (((company[i])-> age) > 49){ans = i;break; /*example of multiple return values from a c function*/ if (ans == (-1)) return 0; else { *a = company[ans]; return 1; ; a company[ ] Barbara Imperative Programming 6 55 instance of employee

Example /* a sample run 128 remus!c> gcc -Wall employee.c 129 remus!c> a.out Barbara Ryder */ Imperative Programming 7

Casting Safe uses of casting For pointers returned from malloc p = (int *) malloc (4); For simulating subtyping safely in C struct s{ struct t{ s is like a subtype of t int a; int a; because it has same int b; int b; fields as t plus an double c; extra field. Imperative Programming 8

newcasting.c /*example due to satish chandra of bell labs this is a use of casting that is like subtyping*/ #include<stdio.h> typedef struct{ int x,y; point; typedef enum{ RED, BLUE color; typedef struct{ int x,y; color c; colorpoint; void translatex(point *p, int dx){ p->x += dx; /*translates x co-ordinate by 1*/ Imperative Programming 9

newcasting.c void main(void){ point p; colorpoint cp; /* initialize p to (0,0) and cp to (1,1) */ p.x = 0; p.y = 0; cp.x = 1; cp.y = 1; cp.c = RED; printf(" p= %d,%d cp= %d,%d\n",p.x,p.y,cp.x,cp.y); /* move x co-ordinate by 1 for both points*/ translatex(&p, 1); translatex((point *) &cp, 1); printf("after translation, p= %d,%d cp= %d,%d\n, p.x,p.y,cp.x,cp.y); Imperative Programming 10

Output /* output resulting 20 scherzo!c> gcc newcasting.c 21 scherzo!c> a.out p= 0,0 cp= 1,1 after translation, p= 1,0 cp= 2,1 22 scherzo!c> */ int int point Why the cast works? Perfect matchup of fields int int color colorpoint Imperative Programming 11

Strings Strings are arrays of chars, as in Pascal Because of special relation between array names and pointers, often see strings defined as char * String library contains useful functions int strcmp(char *s,char *t): returns value < 0 if s is less than t (in lexicographic order), 0 if s == t and >0 if t is less than s. char * strcpy(char *s, char *t): copies the string pointed to by t into the string pointed to by s; to work this needs t to be declared big enough to store the string. Imperative Programming 12

Strings int strcpy (char *s, char *t){ for (; *t!= \0 ; s++,t++) *t = *s; On return, t points to a copy of the string pointed to by s. Means you had to have allocated storage for t BEFORE calling strcpy(). Q: What is the difference in meaning of t = s versus *t = *s?? Imperative Programming 13

Strings and Chars Ascii collating sequence encodes characters representing letters as consecutive integers; therefore this works: char s,t; scanf( %c %c, &s, &t); if (s < t). Imperative Programming 14

Another C example #include<stdio.h> int main(void) { typedef struct point pt; struct point{ int x; int y; ; int i,j; pt *p, *q, *a[15];/*a[ ] stored on main's stack frame*/ float sumx,sumy,sumz; printf("\n points are"); for (i=0; i<15; i++) { p = (pt *)malloc (sizeof (pt)); p->x = i; p->y = i-1; a[i] = p; printf("\n%d %d", p->x,p->y); Imperative Programming 15

/* calculate the center of these points */ sumx = sumy = 0; for (i=0; i<15; i++;q++) { sumx += (*a[i]).x; sumy += (*a[i]).y; sumx = sumx/15.; sumy = sumy/15.; printf("\n center of mass is %f %f", sumx, sumy); printf("\n"); 75 athos!c>!gcc gcc arrayofstructs.c 76 athos!c>./a.out points are 0-1 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 center of mass is 7.000000 6.000000 Imperative Programming 16

typedef struct point pt; struct point{ int x; int y; ; int i,j; pt *p, *q, *a[15]; float sumx,sumy,sumz; /*just trying pointer-array exprs for fun*/ sumz = (*(a+2))->y;/*this is legal; note a+i and a[i] are same loc*/ /* sumz = *(a+2)->y; be careful of operator precedence;this is illegal*/ printf ("sumz is %d\n",sumz); q = *(a+2);/* a+2 is same as &a[2], so we have to derefernce it to get its contents to copy into q*/ sumz = (*q).y; printf ("sumz is %d\n",sumz); sumz is 1 sumz is 1 Imperative Programming 17