1 A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Pointers and References (1) THE REFERENCE OPERATOR REFERENCES POINTERS THE DEREFERENCE OPERATOR DERIVED TYPES RETURNING A REFERENCE.
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.
Multiple-Subscripted Array
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
 Review structures  Program to demonstrate a structure containing a pointer.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
1 Workin’ with Pointas An exercise in destroying your computer.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
2/17/2016IT 2791 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name:
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
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.
#define #include<iostream> using namespace std; #define GO
Pointers & Arrays.
Pointers Psst… over there.
C++ Arrays.
Pointers Psst… over there.
Pointer Basics Psst… over there.
Pointers & Functions.
Functions Pass By Value Pass by Reference
Dynamic Memory A whole heap of fun….
Pointers Lecture 2 Tue, Jan 24, 2006.
Homogeneous aggregate
C++ Pointers and Strings
Pointers & Arrays.
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Basics Psst… over there.
C++ Pointers and Strings
Presentation transcript:

1 A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know so the computer knows how to read or store the contents of the memory. Can be

2 // define pointers int a, *ap; // can’t use a and *a at the same time; double b, *bp; token k, *kp; a is an integer, ap is a pointer to an integer; b is a double, bp is a pointer to a double; k is a token, kp s a pointer to a token. Define pointers

3 A Pointer points to a location of the computer memory int a=5; int *ap; double b=2.3; double *bp; token k; token *kp; bp = new double; *bp = 3.14; *ap = 7; // not new-ed yet a ap b bp k kp 4 bytes } ? ? FFFFA080 FFFFA

4 A Pointer points to a location of the computer memory double a=5.1; double *ap; double *b,*c; ap = &a; // &a :the address of a *ap=1.7; b = new double; *b = 3.14; b = &a; c = b; a ap b c 4 bytes } new FFFFA080 FFFFA

5 The name of an array is a pointer int a[4]={5,60,700,8000}; int *p; p=a; cout << *p << endl; cout << *p+1 << endl; cout << *(p+1) << endl; cout << *(p+2) << endl; cout << *(p+3) << endl; cout << *(p+4) << endl; apap 4 bytes } FFFFA080 FFFFA

6 Call by value double fun(int n, double p) { for (int i=0; i < 2; i++) p = p-n; return p; } npinpi int main() { int i=1,k=4; double p, s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } ikpsikps 1+4=

7 Call by reference double fun(int n, double & p) { for (int i=0;i < 2;i++) p = p-n; return p; } 5 0 npinpi int main() { int i=1,k=4; double p, s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } ikpsikps &s 1+4=

8 Call by Value vs. Call by Reference int add_one_V(int n) { n++; cout << “1:“ << n << endl; return n; } 5 n int main() { int a,b=5; a = add_one_V(b); cout << “2:“ << a << “ “ << b << endl; a = sub_one_R(b); cout << “3:“ << a << “ “ << b << endl; } 1:6 2:6 5 3:4 4 abab int sub_one_R(int & n) { n--; return n; } n &b

9 Passing Arrays as Parameters int top(int a[]) { int i=0; return a[i]; } 0 aiai int main() { int a[4]={10,200,300,400}; cout << top(a) << endl; cout << third(a) << endl; } a int third(int a[]) { int i=2; return a[i]; } 2 aiai a+2

10 Arrays passed as References void fun(int a[]) { int i=2; a[i] += 5; } 2 aiai int main() { int a[4]={10,200,300,400}; fun(a); cout << a[0] << “ “; cout << a[1] << “ “; cout << a[2] << “ “; cout << a[3] << endl; return 1; } a a[2]

11 Pass the size of an Array int main() { const int size=4; int a[size]={1,2,3,4}; add_one_to_all(a,size); for (int i=0;i<size;i++) cout << a[i] << “ “; return 1; } 4 0 size i a 1 2 void add_one_to_all(int A[], int size) { for (int i=0;i<size;i++) A[i]++; } 0 4 i A size

12 Vector passed as Value int main() { vector a(4); int i; for (i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (i=0;i<a.size();i++) cout << a[i] << “ “; } iaia 1 2 void add_one_to_all(vector v) { for (int i=0;i<v.size();i++) v[i]++; } iviv

13 Vector passed as Reference int main() { vector a(4); for (int i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (int i=0;i<a.size();i++) cout << a[i] << “ “; } iaia 1 2 void add_one_to_all(vector & v) { for (int i=0;i<a.size();i++) a[i]++; } 0 iviv &a

14 C-string is an array of char char *a = "Dennis"; char b[7] = {'D','e','n', 'n','i','s','\0'}; char *c=b; cout << *a << “ “ << b[0] << endl; cout << *(a+2) << “ “ << b[2] << endl; cout << a << “ “ << b << endl; char *d[2] = {c, "Li"}; cout << d[0] << " " << d[1] << endl;; abcdabcd 4 bytes } D n Dennis Dennis Li } 1 byte ‘D’ ‘e’ ‘n’ ‘i’ ‘s’ 0 ‘L’ ‘i’ d[0] d[1] ‘D’ ‘e’ ‘n’ ‘i’ ‘s’ 0