Lecture06: Pointers 4/1/2013

Similar documents
Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Goals of this Lecture

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

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

Applications of Pointers (1A) Young Won Lim 3/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/31/18

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

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

14. Memory API. Operating System: Three Easy Pieces

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory

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

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

Definition. Pointers. Outline. Why pointers? Definition. Memory Organization Overview. by Ziad Kobti. Definition. Pointers enable programmers to:

Computer Science & Engineering 150A Problem Solving Using Computers

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

Introduction to Scientific Computing and Problem Solving

Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types

Assist. Prof. Dr. Caner ÖZCAN

Pointers and Memory Allocation p. 1. Brooklyn College. Michael Lampis. CISC 3130 Notes. Pointers and Memory Allocation

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

First of all, it is a variable, just like other variables you studied

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:

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

Fundamental of Programming (C)

Subject: Fundamental of Computer Programming 2068

Programming Language B

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

EM108 Software Development for Engineers

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 1613 Lecture 24. Figure 1. Program p01.

ESC101N Fundamentals of Computing

ESc101: Pointers and Arrays

ESC101N: Fundamentals of Computing End-sem st semester

Lecture 04 Introduction to pointers

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays and Strings. Arash Rafiey. September 12, 2017

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

SYSC 2006 C Winter 2012

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

Memory Management. CSC215 Lecture

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

CS453 Arrays in Java in general

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Understanding Pointers

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Intermediate Programming, Spring 2017*

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

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

Who. Winter Compiler Construction Generic compiler structure. Mailing list and forum. IC compiler. How

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

EL2310 Scientific Programming

APSC 160 Review Part 2

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

today cs3157-fall2002-sklar-lect05 1

CS 11 C track: lecture 5

Procedural programming with C

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

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

Arrays and Pointers (part 1)

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

CS240: Programming in C

EL2310 Scientific Programming

CS16 Exam #1 7/17/ Minutes 100 Points total

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

Review of Important Topics in CS1600. Functions Arrays C-strings

Arrays and Pointers in C. Alan L. Cox

CSC 270 Survey of Programming Languages. What is a Pointer?

Today s Learning Objectives

Introduction to C. Systems Programming Concepts

C Programming Review CSC 4320/6320

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Lesson 7. Reading and Writing a.k.a. Input and Output

CS 261 Data Structures. Introduction to C Programming

Computer Programming Unit 3

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers as Arguments

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Computer Organization & Systems Exam I Example Questions

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

Pointers. Introduction

Arrays and Pointers in C & C++

Basic and Practice in Programming Lab7

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

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

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

C++ For Science and Engineering Lecture 15

a data type is Types

Transcription:

Lecture06: Pointers 4/1/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Pointers A ointer is a variable that contains the (memory) address of another variable What is a memory address? 2 1

Memory Variables are stored in comuter memory Think of memory as a very large array Like an one column excel sheet, each entry/location is a byte. Every byte/location in memory has an address An address is an integer, just like an array index In C, a memory address is called a ointer C lets you maniulate memory locations directly (store, add, subtract,etc.) 3 Pointer Declaration & Usage Pointer Declaration int *1; // int* 1 also okay float *2; unsigned int *3; char *4; void *5; Pointer Usage int a = 10; int *tr; tr = &a; 4 2

Two Oerators for Pointers &( address of ) oerator Returns the address of its argument (i.e., the row number in excel) Said another way: returns a ointer to its argument The argument must be a variable name *( dereference ) oerator Returns the value stored at a given memory address The argument must be a ointer int a = 5; int *a = &a; a 5 a rintf( %d\n, *a); rintf( %\n, a); //? 5 Examle int a = 0; int b = 0; int *; 0 a 0 b a = 10; = &a; rintf( %d\n, *&a); 10 a 0 b * = 20; // a =? b =? 20 a 20 a 0 b = &b; 0 b * = 10; // a =? b =? a = *; // a =? b =? 10 a 20 a 10 b 10 b 6 3

More examle int i, j, *, *q; = &i; q = ; q? i? j * = 1; q 1 i? j *q = 2; q 2 i? j 7 Recall the & in scanf()? int i; scanf( %d\n, &i); Pass the ointer to i, so that scanf() can modify its value. 8 4

What are ointers good for? Passing ointers to functions so that functions can change the values ointed by these ointers void swa(int* c, int* d) int t = *c; *c= *d; *d = t; void main() int a = 5, b = 3; rintf("before swa: a = %d b = %d\n", a, b); swa(&a, &b); rintf("after swa: a = %d b = %d\n", a, b); 9 What are ointers good for? Multile Return Values void initialize(int *a, char *b) *a = 10; *b = 'x'; void main() int a, b; initialize(&a, &b); 10 5

In Class Exercise 5 1 Write a function that finds the largest and smallest elements in an array. This function has the following rototye: void max_min(int a[], int n, int *max, int *min); A call to max_min might be as follows: max_min(b, N, &big, &small); Also write a comlete rogram that reads 10 integers into an array and asses the array to max_min, and rints the results as follows: Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31 Largest: 102 Smallest: 7 11 Exercise 5.1 Solution #include <stdio.h> void max_min(int a[], int n, int *max, int *min) // max = &big; min = &small; int i; *max = a[0]; *min = a[0]; for (i=1; i<n; i++) if (*max < a[i]) *max = a[i]; if (*min > a[i]) *min = a[i]; int main() int b[10]; int i, big, small; rintf("inut 10 numbers:"); for (i=0; i<10; i++) scanf("%d", &b[i]); max_min(b, 10, &big, &small); rintf("largest: %d \nsmallest: %d", big, small); return 1; 12 6

Examle ointer to ointer int i; int *; int **m; // Integer i // Pointer to integer // Pointer to int ointer = &i; // now oints to i rintf("%", ); // Prints the address of i (in ) m = &; // m now oints to rintf("%", m); // Prints the address of (in m) 13 Use const to Protect Arguments /* f cannot change the value of */ void f(const int *) * = 0; /* ERROR! */ 14 7

Pointers as Return Values /* return ointer to whichever integer is larger */ int *max(int *a, int *b) if (*a > *b) return a; else return b; 15 Pointers Are (Very) Dangerous What s wrong with the code? void main() int *; * = 10; rintf( %d, *); int *f() int i; return (&i); 16 8

Pointers Are (Very) Dangerous What s wrong with the code? void main() char x = 'a'; char * = &x; ++; rintf("%c\n", *); 17 Recall: Arrays To declare an array, use [], e.g.: int a[5]; // Creates an array with 5 integer elements The size of an array can't be changed The number between the brackets must be a constant You can give initial values for array elements, e.g: int a[5] = 3, 7, 1, 4, 6; A better way: int a[] = 3, 7, 1, 4, 6; // Let the comiler calculate the size 18 9

Recall: Arrays Array indices in C are zero based, e.g. a[0], a[1],..., a[4] void main() int a[] = 3, 7, -1, 4, 6; int i; double average = 0; // comute the average of values in an array for (i = 0; i < 5; ++i) average += a[i]; average /= 5; // average = average / 5; rintf( Average = %.2f\n", average); 19 Pointers and Arrays Pointers and arrays are closely related An array variable is actually just a ointer to the first element in the array You can access array elements using array notation or ointers a[0] is the same as *a a[1] is the same as *(a + 1) a[2] is the same as *(a + 2) You can add or subtract ointers like integers 20 10

Adding an integer to a Pointer int a[10]; int *, *q, i; = a; a 0 1 2 3 4 5 6 7 8 9 = &a[2]; q = + 3; a 0 1 2 3 4 5 6 7 8 9 q += 6; a 0 1 2 3 4 5 6 7 8 9 21 Pointers and Arrays Access array elements using ointers void main() int a[] = 3, 7, -1, 4, 6; int i; double average = 0; // comute the average of values in an array for (i = 0; i < 5; ++i) average += *(a + i); average /= 5; rintf( Average= %.2f\n", average); 22 11

Pointers and Arrays If a oints to a articular element of an array, (a + 1) always oints to the next element, (a + i) oints i elements after a and (a - i) oints ielements before. The only difference between an array name (a) and a ointer (a): A ointer is a variable, so a = a and a++ are legal An array name is not a variable, so a = a and a++ are illegal int a[10]; int *a; a = a; a++; // okay a = a; a++; // wrong!! 23 Pointers, Arrays and Functions It's ossible to ass art of an array to a function, by ass a ointer to the beginning of the sub array. f(&a[2]) f(a + 2) Within f, the arameter declaration can read f(int arr[])... f(int* arr)... 24 12

In Class Exercise 5 2 Comlete the following rogram that rints the characters in a reverse order. char s[10] = "abcde"; char* ctr; for (ctr = &s...) rintf("%c", *ctr); 25 Dynamically Allocating Arrays malloc: Allocate contiguous memory dynamically int n; if ( ) n =10; else n = 5; int* = (int*) malloc(n * sizeof(int)); int n = 5; int [n]; An array of size n free: De allocate the memory free(); Make sure malloc and free are aired! 26 13

In Class Exercise 5 3 Modify the code in Exercise 5 1 to ask for how many numbers. Use malloc() and free() to allocate the size of the array. Enter N: 10 Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31 Largest: 102 Smallest: 7 27 Exercise 5.3 Solution #include <stdio.h> void max_min(int a[], int n, int *max, int *min) // max = &big; min = &small; int i; *max = a[0]; *min = a[0]; for (i=1; i<n; i++) if (*max < a[i]) *max = a[i]; if (*min > a[i]) *min = a[i]; int main() int* b; int i, big, small; b = (int *)malloc(10*sizeof(int)); rintf("inut 10 numbers:"); for (i=0; i<10; i++) scanf("%d", &b[i]); max_min(b, 10, &big, &small); rintf("largest: %d \nsmallest: %d", big, small); free(b); return 1; 28 14