Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointers Applications
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Computer Skills2 for Scientific Colleges
Pointers.
Pointers Introduction
Pointer.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Computer Skills2 for Scientific Colleges
CS111 Computer Programming
C Programming Lecture-8 Pointers and Memory Management
Pointers and pointer applications
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Pointer Arithmetic CSE 2541 Rong Shi

Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address Memory addresses – Z + – Positive, whole number Why do pointers have an associated type? Ex: – int *pfloat * pchar * p

From earlier material… intarray[10]; int*ap = array + 2; // ap points at &array[2] 1. ap 2.*ap 3.ap[0] 4.ap *ap *(ap + 6) 7.ap[6] 8.&ap 9.ap[-1] //ap[-3]? 10.ap[9]

Pointer arithmetic example int array[] = { 1, 2, 3 }; int *array_ptr = array; // try different types printf(“First: %i\n”, *(array_ptr++)); // printf(“First: %i\n”, *(++array_ptr)); printf(“Second: %i\n”, *(array_ptr++)); printf(“Third: %i\n”, *array_ptr);

Pointer arithmetic When you add to or subtract from a pointer, the amount by which you do that is multiplied by the size of the type the pointer points to. For the previous slide, each increment adds 1 times sizeof(int) – p++=>value of p = old value of p + 4

Examples

NULL vs 0 vs ‘\0’ NULL is a macro defined in several standard headers – Only used for pointers – May be defined as ((void *) 0) 0 is an integer constant – 0 acts as the generic symbol for every type’s zero value '\0' is a character constant – Referred to as nul – Only exists if you #define it yourself

NULL and void – ((void *) 0) 0 – the value of a NULL pointer (using casting) VOID – no type – Cannot use arithmetic on a void pointer (no sizeof) – Cannot dereference (no type to return / unable to determine how many bytes to consider) NULL is defined as 0 cast to a void * pointer(NOT an uninitialized pointer) Note that NULL and void are separate concepts char *p=0; char *t=NULL; int i=0; char *q=(char*)i; // char * not the same type/sizeof as int * char *q=0 char *q=(char*)0

L and R values L-value (appearing on the left side of an assignment) – A place (i.e. memory location) for a value to be stored R-value (appearing on the right side of an assignment) – A value or expression that simplifies to a value a = b+25 vs b+25 = a int a[30]; a[b+10]=0; int a, *pi; pi = &a; *pi = 20;

L and R values Given: char ch = ‘a’; char *cp = &ch;

Pointers to Arrays (* )[ ] Ex: a pointer ptr to an array of 5 integers. – int(*ptr)[5]; Ex: an array of 5 int pointers – int *ptr[5];