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

Similar documents
CS113: Lecture 4. Topics: Functions. Function Activation Records

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

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

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

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

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

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

CA31-1K DIS. Pointers. TA: You Lu

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

Decimal Representation

Arrays and Pointers (part 1)

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

EM108 Software Development for Engineers

MYcsvtu Notes LECTURE 34. POINTERS

Arrays and Pointers. CSE 2031 Fall November 11, 2013

At the end of this module, the student should be able to:

Intermediate Programming, Spring 2017*

Lecture 04 Introduction to pointers

DECLARAING AND INITIALIZING POINTERS

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

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

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

Arrays and Pointers (part 1)

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

Intermediate Programming, Spring 2017*

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

Dynamic memory allocation (malloc)

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

Chapter 7 C Pointers

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

Subject: Fundamental of Computer Programming 2068

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

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

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

What have we learned about when we learned about function parameters? 1-1

CS 61c: Great Ideas in Computer Architecture

Intro to C: Pointers and Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

SYSC 2006 C Winter 2012

Arrays, Pointers and Memory Management

Multidimension array, array of strings

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

Expressions and Casting

[0569] p 0318 garbage

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

BITG 1113: POINTER LECTURE 12

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

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

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Goals of this Lecture

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

printf( Please enter another number: ); scanf( %d, &num2);

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Variation of Pointers

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

Structures and Pointers

pointers + memory double x; string a; int x; main overhead int y; main overhead

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers


Algorithms & Data Structures

CS 237 Meeting 18 10/22/12

Lecture 05 POINTERS 1

Variables, Pointers, and Arrays

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

BSM540 Basics of C Language

ESC101N Fundamentals of Computing

Object-Oriented Principles and Practice / C++

Procedural programming with C

A brief intro to pointers for the purposes of passing reference parameters

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

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

Functions. Arash Rafiey. September 26, 2017

Introduction to Scientific Computing and Problem Solving

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

ESc101: Pointers and Arrays

Lesson #8. Structures Linked Lists Command Line Arguments

Basic Pointers. CSCI 112: Programming in C

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS31 Discussion 1E Spring 17 : week 08

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Chapter 6 - Pointers

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

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

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

REFERENCES, POINTERS AND STRUCTS

Arrays and Pointers in C. Alan L. Cox

Practice question Answers

Computer Programming Lecture 12 Pointers

CS 222: Pointers and Manual Memory Management

Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Transcription:

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

From Last Time: A Useless Function #include <stdio.h> void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n", age ); void get_age( int other_age ) { printf( "Please enter your age.\n" ); scanf( "%d", &other_age ); This is a contrived example, because we could just have get age return the age as an integer. But what if we want a function to modify the contents of a variable we pass to it? Suppose you want a function to sort an array of numbers, or swap the contents of two memory locations? And how does scanf work? (Remember the & that comes before the variable you pass to scanf?) 2

From last time: the execution stack get_age() activation record 21 other_age function call creates the new record main() activation record?? age Remember that when get_age() finishes, its activation record is destroyed, so the value stored in other_age is lost. Can we do any better? 3

What if? Suppose that when main() calls get age(), we passed a pointer to the age variable in main(), so that get age() knew about that variable. get_age() activation record pointer to "age" main() activation record?? age Then, perhaps, we could tell get age() to modify the variable that it points to. In fact, C lets us do exactly this, using pointers. 4

Introduction to Pointers A variable in a program is stored in a certain number of bytes at a particular memory location, or address, in the machine. Pointers allow us to manipulate these addresses explicitly. To declare a pointer variable: add a star to the type you want to point to. Example: int *a; declares a variable a of type int *, which can be used to hold the address of (or a pointer to ) an int. Two unary operators ( inverses ): & operator - address of operator. Can be applied to any variable. Type of resulting expression has one more star than original expression. * operator - dereference operator. Can be applied only to expressions that represent memory locations. Accesses the object that the pointer points to. Type of resulting expression has one less star than original expression. 5

Pointers: Example 1 int x = 1, y = 2, z = 3; int *ip; ip = &x; z = *ip; Here s what it looks like after the last statement: ip main() activation record 1 2 z y 1 x 6

Don t Get Confused! Pointer notation is pretty confusing: When we declare int *a, we re saying that the type of a is pointer to variables of type int. It might be less confusing if we wrote int* a, to emphasize that the type of a is int*, but this isn t usually how C programmers write the declaration. (It works, though.) The * operator dereferences the pointer to get at the variable we re pointing to. It makes the pointer less of a pointer, whereas the * in the declaration makes the type more of a pointer. In short: don t confuse the * operator with the * in the declaration of a pointer variable (or with multiplication)! 7

int x = 1, y = 2; int *ip; char c; char *cp; Pointers: Example 2 ip = &x; /* ip now points to x */ printf( "%d\n", *ip ); /* prints 1 */ printf( "%d\n", *ip + 2 ); /* prints 3 */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ printf( "%d\n", x ); /* prints 0 */ cp = &x; /* doesn t work; types don t match */ *cp = z ; /* what happens? */ cp = &c; *cp = z ; printf( "%c\n", c ); /* prints z */ 8

printf vs. scanf int k; printf( "Enter an integer: " ); scanf( "%d", &k ); printf( "%d", k ); This also works scanf is happy as long as it gets a pointer: int k, *pk; pk = &k; printf( "Enter an integer: " ); scanf( "%d", pk ); printf( "%d", k ); Who wants what information? printf( "%d",... ); expects an int, since it needs to know what to print out scanf( "%d",... ); expects the address of an int, since it needs to know where to place the int typed in scanf doesn t care about the actual value of the int that it should write to 9

int a = 3, b = 3; int *pa, *pb; pa = &a; pb = &b; More practice if( pa == pb ) printf( "pa and pb are equal.\n" ); if( *pa == *pb ) printf( "*pa and *pb are equal.\n" ); (*pa)++; /* careful: different from *pa++ */ *pb += *pa; printf( "a: %d, b: %d\n", a, b ); pb = pa; *pa += *pb; printf( "a: %d, b: %d\n", a, b ); if( pa == pb ) printf( "pa and pb are equal.\n" ); if( *pa == *pb ) printf( "*pa and *pb are equal.\n" ); /* super tricky */ *((0 > 1)? &a : &b) = 5; 10

How to swap two values? What s wrong with this? void swap( int x, int y ) { int temp; temp = x; x = y; y = temp; int a = 3, b = 5; swap( a, b ); printf( "a is %d, b is %d\n", a, b ); 11

A correct swap void swap( int *px, int *py ) { int temp; temp = *px; *px = *py; *py = temp; int a = 3, b = 5; swap( &a, &b ); printf( "a is %d, b is %d\n", a, b ); 12

Be careful with your new toys. When you re using pointers, always think of the activation records that will be generated by the program! Do not point at expressions that are not variables. int k = 1, *ptr; ptr = &3; /* illegal */ ptr = &(k + 99); /* illegal */ Do not try to dereference non-pointer variables. int k; printf( "%d", *k ); /* illegal */ What s wrong with this? int *function_3() { int b; b = 3; return &b; int *a; a = function_3(); printf( "a is equal to %d\n", *a ); When a function returns, its activation record (along with the data it contains) gets destroyed! 13

int a, b; int *pc, *pd; int **ppe, **ppf; An example a = 3; b = 5; pc = &a; pd = &b; (*pd)++; printf( "a: %d b: %d\n", a, b ); *pc += *pd; printf( "a: %d b: %d\n", a, b ); ppe = &pc; ppf = &pd; *ppf = pc; *pd = 12; printf( "a: %d b: %d\n", a, b ); **ppe = 50; **ppf = 15; printf( "a: %d b: %d\n", a, b ); 14