1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE1301 Computer Programming Lecture 16 Pointers.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Programming in C Pointer Basics.
Pointers CSE 2451 Rong Shi.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© 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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Variables and memory addresses
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,
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
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. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
Dynamic Storage Allocation
Pointers Introduction
Pointers and Pointer-Based Strings
Values – also known as scalars (eg. int, double, char, float)
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Programmazione I a.a. 2017/2018.
Pointer.
Programming in C Pointer Basics.
Topics discussed in this section:
Programming in C Pointer Basics.
Programming in C Pointer Basics.
Chapter 16 Pointers and Arrays
Pointers and Pointer-Based Strings
Programming in C Pointer Basics.
Introduction to Pointers
Presentation transcript:

1 Chapter Thirteen Pointers

2 Pointers A pointer is a sign used to point out the direction

3 Pointers A pointer is a data item whose value is the address in memory of some other value

4 Pointers Allow you to refer to a large data structure in a compact way Facilitate sharing data between different parts of a program Make it possible to reserve new memory during program execution Can be used to record relationships among data items

5 Variables Each variable refers to some location in memory and therefore has an address Once a variable has been declared, the address of the variable never changes, even though the content of the variable may change Depending on the type of data they contain, different variables require different amount of memory

6 Lvalue and Rvalue x = x; Store the content of the memory location at address 1000 to the memory location at address x: Lvalue: address Rvalue: content

7 Lvalue-Expressions An expression that refers to a memory location capable of storing data has an lvalue x = 1.0; intarray[2] = 17; Many expressions do not have lvalues 1.0 = 1.0;/* illegal */ x = 17;/* illegal */

8 Lvalue-Expressions Each lvalue-expression refers to some location in memory and therefore has an address Once it has been declared, the address of an lvalue-expression never changes, even though the contents of the lvalue-expression may change Depending on the type of data they contain, different lvalue-expressions require different amount of memory The address of an lvalue-expression is itself data that can be manipulated and stored in memory

9 Pointer Declarations Pointers can be declared as base-type * pointer-variable; int *iptr; char *cptr; int *p1, *p2; int *p1, p2;

10 Pointer Operations & : address-of returns the address of an lvalue-expression int x, *p; p = &x; p = &8;/* Illegal */ * : value-pointed-to (dereferencing) refers to the memory location pointed to by a pointer int x, *p; p = &x;/* *p  x */ x = *p;

11 Examples int x, y; int *p1, *p2; x = -42; y = 163; p1 = &x; p2 = &y; x: y: p1: p2:

12 Examples /* *p1  x, *p2  y */ *p1 = 17; /* *p1  y, *p2  y */ p1 = p2; /* *p1  y, *p2  y */ *p1 = *p2; x: y: p1: p2: x: y: p1: p2: x: y: p1: p2:

13 The Special Pointer NULL In many applications, it is useful to be able to store in a pointer variable a special value indicating that the variable does not in fact point to any valid memory location The special constant NULL is defined for this purpose It is important not to dereference a pointer variable that has the value NULL or is not initialized with the * operator

14 Passing Parameters by Value void setToZero(int var) { var = 0; } main() { int x; x = 10; setToZero(x); } var: 10 x: 10 var: 0 x: 10

15 Passing Parameters by Reference void setToZero(int *ip) { *ip = 0; } main() { int x; x = 10; setToZero(&x); } ip: x: 10 ip: x: 0

16 An Example void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; }

17 Returning Multiple Results void convertTimeToHM(int time, int *pHours, int *pMinutes) { *pHours = time / MinutesPerHour; *pMinutes = time % MinutesPerHour; } main() { int time, hours, minutes; scanf(“%d”, &time); convertTimeToHM(time, &hours, &minutes); printf(“HH:MM format: %d:%d\n”, hours, minutes); }

18 Don’t Overuse Call by Reference int hours(int time) { return time / MinutesPerHour; } int minutes(int time) { return time % MinutesPerHour; } main() { int time; scanf(“%d”, &time); printf(“HH:MM format: %d:%d\n”, hours(time), minutes(time)); }

19 Pointers and Arrays Pointers can also point to elements of an array int array[10], *p; p = &array[0]; *p = 10; printf(“%d, %d\n”, array[0], *p);

20 Pointer Arithmetic If a pointer points to elements of an array, some simple pointer arithmetic is meaningful If p points to array[i], p+k points to array[i+k] If p points to array[i], p-k points to array[i-k] If p points to array[i] and q points to array[j], p-q is equal to i-j

21 Pointer Arithmetic array[0] array[1] array[2] p1 p2 p1-2, p2 p1-1, p2+1 p1, p2+2

22 An Example main() { int i, sum, array[10], *p; for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (p = &array[0]; p <= &array[9]; p++) { sum += *p; } main() { int i, sum, array[10]; for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (i = 0; i < 10; i++) { sum += array[i]; }

23 ++ and -- The postfix form: x++ uses the value of x as the value of the expression first, and then increments it The prefix form: ++x increments the value of x first, and then uses the new value as the value of the expression

24 An Example main() { int x, y; x = 5; y = ++x; printf(“x = %d, y = %d\n”, x, y); x = 5; y = x++; printf(“x = %d, y = %d\n”, x, y); }

25 An Example for (i = 0; i < n; i++) arr[i] = 0; for (i = 0; i < n;) arr[i++] = 0; for (i = 0; i < n;) arr[i] = i++;

26 An Example *p++ (*p)++*(p++)

27 An Example main() { int i, sum, array[10], *p; for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (p = array; p <= &array[9];) { sum += *p++; } main() { int i, sum, array[10]; for (i = 0; i < 10; i++) { scanf(“%d”, array+i); } sum = 0; for (i = 0; i < 10; i++) { sum += *(array+i); }

28 An Example int add(int array[], int size) { int i, sum; sum = 0; for (i = 0; i < size; i++) sum += array[i]; return sum; } main() { int s, n[SIZE]; s = add(n, SIZE); } int add(int *array, int size) { int i, sum; sum = 0; for (i = 0; i < size; i++) sum += *(array+i); return sum; } main() { int s, n[SIZE]; s = add(n, SIZE); } 

29 An Example main() { int i, sum, array[10]; for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (i = 0; i < 10; i++) { sum += array[i]; } printf(“%d\n”, sum); } main() { int i, sum, *array; for (i = 0; i < 10; i++) { scanf(“%d”, array+i); } sum = 0; for (i = 0; i < 10; i++) { sum += *(array+i); } printf(“%d\n”, sum); } error 4 bytes40 bytes

30 Dynamic Allocation Static allocation: memory spaces that are allocated in fixed locations and persist throughout the entire program Automatic allocation: memory spaces that are allocated when entering a function and freed when exiting a function Dynamic allocation: memory spaces that are explicitly allocated and freed by programmers while the program is running

31 Memory Organization Static area Stack area Heap area

32 Malloc and Free In stdlib.h: void *malloc(int nBytes); void free(void *pointer); void * is a general pointer type

33 Malloc and Free char *cp; cp = (char *) malloc(10 * sizeof(char)); free(cp); cp int *ip; ip = (int *) malloc(10 * sizeof(int)); free(ip); ip

34 Dynamic Arrays main() { int i, sum, n, *array; scanf(“%d”, &n); array = (int *) malloc(n * sizeof(int)); for (i = 0; i < n; i++) scanf(“%d”, array+i); /* scanf(“%d”, &array[i]) */ sum = 0; for (i = 0; i < n; i++) sum += *(array+i); /* sum += array[i] */ printf(“%d\n”, sum); free(array); } dynamic array

35 Detecting Errors in Malloc main() { int i, sum, n, *array; scanf(“%d”, &n); array = (int *) malloc(n * sizeof(int)); if (array == NULL) { printf(“Error: no more memory\n”); exit(1); } for (i = 0; i < n; i++) scanf(“%d”, array+i); sum = 0; for (i = 0; i < n; i++) sum += *(array+i); printf(“%d\n”, sum); }