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

Similar documents
DECLARAING AND INITIALIZING POINTERS

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

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

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

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

Pointers. Memory. void foo() { }//return

Fundamentals of Programming Session 20

Pointers and Arrays 1

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CS201- Introduction to Programming Current Quizzes

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Fundamentals of Programming Session 20

Arrays and Pointers in C. Alan L. Cox

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

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

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

CS 61c: Great Ideas in Computer Architecture

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 Arrays, Strings, More Pointers

POINTER & REFERENCE VARIABLES

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

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

ECE 15B COMPUTER ORGANIZATION

Computer Systems Principles. C Pointers

Lecture 04 Introduction to pointers

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Pointers. 10/5/07 Pointers 1

by Pearson Education, Inc. All Rights Reserved.

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

More about BOOLEAN issues

[0569] p 0318 garbage

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

Homework #3 CS2255 Fall 2012

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

PROGRAMMAZIONE I A.A. 2017/2018

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Pointers. Pointers. Pointers (cont) CS 217

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

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

Lecture 05 POINTERS 1

Arithmetic Expressions in C

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

In Java we have the keyword null, which is the value of an uninitialized reference type

CS C Primer. Tyler Szepesi. January 16, 2013

CS31 Discussion 1E Spring 17 : week 08

Programming in C - Part 2

C++ for Java Programmers

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

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

Assist. Prof. Dr. Caner ÖZCAN

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

Pointers and scanf() Steven R. Bagley

Pointers as Arguments

ESC101N Fundamentals of Computing

C++ for Java Programmers

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Introduction to Scientific Computing and Problem Solving

MYcsvtu Notes LECTURE 34. POINTERS

Data Types and Variables in C language

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Fundamental of Programming (C)

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Intermediate Programming, Spring 2017*

Binghamton University. CS-211 Fall Pointers vs. Arrays

Variation of Pointers

Review for Test 1 (Chapter 1-5)

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Personal SE. Arrays Pointers Strings

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Binghamton University. CS-211 Fall Pointers

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

C introduction: part 1

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

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?!

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Multi-Dimensional arrays

Computer Programming Unit 3

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

HW1 due Monday by 9:30am Assignment online, submission details to come

Transcription:

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

About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of pointer Usage: Many data structures are dynamic, and their shapes change from time to time The use of pointers allows us to change the shapes, in a very flexible way 2

Example: A Dynamic List Suppose we have some people, who are waiting in a line to buy Disneyland tickets But from time to time, these people may bring in friends to line after them 3

Example: A Dynamic List To maintain the ordering of the people in the line, we can obviously use an array However, there will be problems Insertion after requires O(n) time in the worst case Later, we will study dynamic list Insertion after can be done in O(1) time 4

What are Pointers? Consider an array A with 10 integers We can access each entry of A by specifying its location ( Ex: A[3], A[9] ) Also, we can get or modify the content of an entry ( Ex: y = A[3]; A[9] = 113; ) 5

What are Pointers? In fact, our memory is just a long array Like a normal array, each entry has a location (or an address), and contains space for storing data To access an entry in our memory, we can use a pointer to specify its location 6

What are Pointers? In C, we declare a pointer using the following syntax : int *ptr ; The above line declares a variable ptr, which is used to point at a location in the memory for storing an integer Similarly, we can also do something like : char *cptr ; 7

Pointers in Action Once we have declared a pointer, we can do something like : ptr = 0; The above line tells ptr to point to the location 0 in the memory This doesn t seem very useful, since there is no particular reason why we want to access location 0 in the memory 8

Pointers in Action As mentioned, our memory is an array Each variable that we declare occupies a certain location in the memory Ex : When we declare int a ; then a certain part of memory will be used by a a 9

Pointers in Action In C, the location of a in the memory can be obtained by &a Then we can write something like : ptr = &a ; which tells ptr to point to the location of a in the memory 10

Pointers in Action In C, when a pointer ptr points to a location in the memory, we can get the value stored in that location by *ptr int a, b, *ptr ; ptr = &a ; a = 5 ; printf( value pointed by p: %d\n, *ptr); a = 8 ; printf( value pointed by p: %d\n, *ptr); In C, *p is called dereferencing of a pointer ptr 11

Pointers in Action In C, we can also get or modify the content in the location pointed by a pointer ptr The syntax is as follows : b = *ptr ; *ptr = 15 ; The first line changes the content of b to the content pointed by ptr The second line changes the content pointed by ptr to be 15 12

Pointers in Action What will happen in the following code? int a, *ptr ; ptr = &a ; a = 5 ; printf( value pointed by p: %d\n, *ptr); *ptr = 15 ; printf( value pointed by p: %d\n, *ptr); printf( value stored by a: %d\n, a); 13

Pointers in Action What will happen in the following code? int a, b, *ptr ; a = 5 ; b = 3 ; ptr = &a ; *ptr = 21 ; ptr = &b ; *ptr = 15 ; printf( value stored by a: %d\n, a); printf( value stored by b: %d\n, b); 14

Although *ptr usually refers the content of the location pointed by ptr, an exception is during declaration The statement : int *ptr = 0 ; Remarks is exactly the same as int *ptr ; ptr = 0 ; 15

Address of Variable In C, each variable has a location in the memory for storing its content It is true even for a pointer variable!! What will happen? int a, *ptr = 0 ; printf( the value of ptr: %x\n, ptr); printf( address of ptr: %x\n, &ptr); ptr = &a ; printf( the value of ptr: %x\n, ptr); printf( address of ptr: %x\n, &ptr); 16

Address of Variable Each entry in an array also has an address What will happen in the following code? int a[10] ; printf( address of a[0]: %x\n, &(a[0])); printf( address of a[1]: %x\n, &(a[1])); In fact, the array name is a constant pointer to the location of its first entry printf( the value of a: %x\n, a); 17

Pointer Arithmetic The entries of an array in C occupies contiguous locations in the memory When a pointer points to a certain entry in an array, we can increment the pointer to point to the next entry int a[10], *ptr ; ptr = a ; ptr++ ; // same as ptr = &(a[0]); // ptr now points at a[1] ptr = ptr + 1; // ptr now points at a[2] 18

Pointer Arithmetic In fact, we can do more : int a[10], *ptr ; ptr = a ; // same as ptr = &(a[0]); ptr = ptr + 3; // ptr now points at a[3] printf( a[7] = %d\n, *(ptr + 4)); // note: ptr still points at a[3] Similarly, we can decrement a pointer to point back to the previous entry 19

Remarks When we add 1 to ptr, the actual value stored ptr may not be increased by 1 Reason : this operation is for a change in the memory location, and the change depends on the type of thing pointed by ptr int a[10], *ptr = a ; printf( value of ptr: %x\n, ptr); printf( value of ptr + 1: %x\n, ptr + 1); Note: In a 32-bit machine, the change is 4, since each integer occupies 4 bytes in the memory 20

Segmentation Fault A pointer allows us to access freely any location in the memory However, some part of the memory is forbidden (ex: it may be running our OS) When we try to touch the content in a forbidden area, segmentation fault occurs int *ptr = 0 ; printf( value of ptr: %x\n, ptr); printf( value pointed by ptr: %d\n, *ptr); 21

In C, we are allowed to perform casting to view a variable as a different type from its declared type char a; int b ; a = A ; Casting and Bus Error b = (int) a ; // casting a as int type printf( value of b: %d\n, b); 22

Casting and Bus Error We can also cast pointers char a[4]; int *ptr ; a[0] = a[1] = a[2] = \0, a[3]= A ; ptr = (int *) a ; printf( value pointed by ptr: %d\n, *ptr); The above is like : a 0 0 0 A 23

Casting and Bus Error However, we need to be very careful What will happen in the following code? char a[4]; int *ptr ; a[0] = 0, a[1] = 0, a[2]= 0, a[3]= 65; ptr = (int *) &(a[1]) ; printf( value pointed by ptr: %d\n, *ptr); A bus error occurs, because we try to deference an integer pointer at a location that is impossible for storing an integer 24