CS 100Lecture 221 Announcements P5 due Thursday Final Exam: Tuesday August 10, 8AM - 10AM, Olin 155 Want references? Remember to drop off a picture and.

Slides:



Advertisements
Similar presentations
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
Introduction to C Programming CE
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 100Lecture 211 Announcements P5 due on Thursday FINAL EXAM Tuesday August 10, 8AM, Olin 155 Review sessions on Thursday and Friday Final grades posted.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
CSC Programming for Science Lecture 36: Structures.
ECE Application Programming
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Object Oriented Programming Lecture 2: BallWorld.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Computer Organization and Design Pointers, Arrays and Strings in C
CSE 374 Programming Concepts & Tools
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

CS 100Lecture 221 Announcements P5 due Thursday Final Exam: Tuesday August 10, 8AM - 10AM, Olin 155 Want references? Remember to drop off a picture and note. Check your grades on the door of Upson 5141

CS 100Lecture 222 Today’s Topics More C Data structures Type definitions Arrays Struct and array parameters Strings in C

CS 100Lecture 223 Recall Pointer Parameters // Swap contents of variables pointed to by x and y void swap (int * x, int * y) {int tmp; tmp= *x; *x= *y; *y= tmp;} Example of a call on procedure swap: int j= 17; int k= 24; swap(&j, &k);

CS 100Lecture 224 Type Definitions and structs C provides a way to group a bunch of variables together, in what is called a struct. A typedef is similar to a class in Java, but the typedef doesn’t have methods CCorresponding Java typedef struct {public class Pair { int a;int a; char ch;char ch; } Pair;}

CS 100Lecture 225 Use of types/structs Pair abc, xyz; xyz.ch= ‘q’; abc.a = 5; Declaration of a struct variable in C actually creates the structure. No separate allocation is needed, as it is in Java (using new …). struct variables may be local to a function,just like ints, chars, and doubles.

CS 100Lecture 226 Struct parameters Structs in C behave exactly like ints and chars in Java. In declaring a variable of some struct type, no reference is created. A struct variable is passed by value. typedef struct { … } Pair; void fv (Pair p) {p.a= 42;} void main (void) { Pair pr; pr.a= 17; pr.ch= ‘?’; fv(pr) }

CS 100Lecture 227 Struct Pointer Parameters If you want to change a component of a struct variable, pass a pointer to the struct typedef struct { … } Pair; // As before void fr (Pair * p) {(*p).a= 42;} void main (void) { Pair pr; pr.a= 17; pr.ch= ‘?’; fr(&pr) } Abbreviation. In C, instead of writing ( * p). a one can write p ->a

CS 100Lecture 228 malloc -- Dynamic Storage Allocation The standard library function malloc is used to obtain a block of storage. It is an extremely low-level function ---its argument is the number of bytes or words to be allocated (typically obtained using operator sizeof). It returns an untyped pointer, which must be cast to the desired type. Allocated storage must be explicitly freed by calling procedure free. There is no automatic garbage collection

CS 100Lecture 229 Example with malloc #include Pair * p; /* p is a pointer to a Pair*/ p= (Pair *) malloc( sizeof(Pair) ); p->a= 17; … free(p);

CS 100Lecture 2210 C Arrays C arrays are syntactically similar to arrays in Java, but the semantics (meaning) is different. Declaring an array in C actually allocates it. Declaring an array of structs allocates a block of storage with space for all the structs in the array. The array name is effectively a pointer to the first element of the array. Array indices start at 0. There is no built-in function to determine the length of an array.

CS 100Lecture 2211 Array examples int b[10]; Pair v[10]; int k; for (k= 0; k < 10; k= k+1) b[k]= 0; v[0].a= 10; v[2].ch= ‘!’; EVIL SYNTAX THAT C ALLOWS: 1[b] = 33; x = 5[b]; UGLY UGLY UGLY!

CS 100Lecture 2212 Arrays as parameters When an array name is used as a parameter, the “value” of the parameter is a pointer to its first element. Therefore, array parameters are always pointers. The called function refers to the argument array directly. No copy of the array is made. An & is usually not needed or appropriate for an array argument

CS 100Lecture 2213 Example of array parameter /* Set s[0..n-1] to 0.0 */ void zero (double a[ ], int n) { int k; for (k= 0; k<n; k= k+1) a[k]= 0.0; } void main(void) { double b[100]; zero(b, 100); }

CS 100Lecture 2214 Arrays and Pointers Because array names are treated as pointers, C programmers tend to use pointers and arrays somewhat interchangeably. Procedure zero could have been written with a pointer parameter, as shown on the next slide. It could still be called with an array name as argument.

CS 100Lecture 2215 Array parameter as pointer /* Set s[0..n-1] to 0.0 */ void zero (double * a, int n) { int k; for (k= 0; k<n; k= k+1) a[k]= 0.0; } void main(void) { double b[100]; zero(b, 100); }

CS 100Lecture 2216 Arrays of structs as parameters An array of structs is treated like any other array. If a parameter is an array of structs, a pointer to the begin-ning of an array (i.e.an array name) is the appropriate argument. /* Store 0’s and blanks in pa[0..n-1] */ function zap (Pair pa [ ], int n) { int k; for (k= 0; k<n; k++) {pa[k].a= 0; pa[k].ch= ‘ ’;} void main( void ) { Pair ray[42]; /* initialize ray[0..16] to 0’s and blanks */ zap(ray, 17); }

CS 100Lecture 2217 Structs containing arrays as parameters Structs may contain arrays --but remember that struct arguments will be copied unless pointers are used. typedef struct { int a[10]; } Ray; void isCopied(Ray r) { r.a[2]= -1; /* Doesn’t change the argument */ } void notCopied(Ray * r) { r.a[2]= -1; /* Changes the argument */ } void main (void) { Ray a; isCopied(a); notCopied(&a); }

CS 100Lecture 2218 Strings in C In C, a string is an array of characters. The array contains the characters in the string followed by an extra char (byte) containing a binary zero. Example: “hello” is represented by an array: h e l l o \0

CS 100Lecture 2219 More on Strings Because strings are arrays, and because array names are basically pointers, the assignment and comparison operations manipulate pointers, and not the strings themselves. char s[ ]= “hello”; char t[ ]= s; /* s and t point to same place*/ s[1]= ‘?’; /* s and t are now “h?llo” */ Standard library strlib.h contains routines to copy string values, catenate them, compare them, etc.

CS 100Lecture 2220 END OF NEW MATERIAL Tomorrow, Friday: review, review, review Check your grades on the door of 5141 Upson this week! Remember: Last chance to see me about your final grade is Wednesday before 2PM Final is Tuesday at 8AM, Olin 155. Bright and early!