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.

Slides:



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

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
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.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
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 CSE 2451 Rong Shi.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
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: 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:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
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”
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 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.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Standard Version of Starting Out with C++, 4th Edition
Pointers and Pointer-Based Strings
Pointers Psst… over there.
void Pointers Lesson xx
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Pointers Lecture 1 Thu, Jan 15, 2004.
Pointers and Pointer-Based Strings
C Programming Pointers
Pointers and dynamic objects
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Pointers.
Introduction to Pointers
Presentation transcript:

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 the same as the variable whose address is being stored; a pointer to int is not type int. You might think a pointer data type would be called something like pointer or ptr. Some common uses of pointers: Accessing array elements Passing arguments by reference to a function ( updatable arguments) Passing arrays and strings to functions Obtaining memory from the system Creating data structures such as linked lists

Addresses and Pointers  Every byte in the computer’s memory has an address.  Addresses are numbers, just as they are for houses on a street. The numbers start at 0 and go up from there—1, 2, 3, and so on.  If you have 1MB of memory, the highest address is 1,048,575.  When a program is loaded into memory, it occupies a certain range of these addresses.  That every variable and every function in the program starts at a particular address.  You can find the address occupied by a variable by using the address-of operator &.

Memory addresses.

The Address-of Operator & // Dealing with addresses of variables #include int main() { int var1 = 11; //define and initialize int var2 = 22; //three variables int var3 = 33; cout << &var1 << endl //print the addresses << &var2 << endl //of these variables << &var3 << endl; return 0; } 0x23ff74 0x23ff70 0x23ff6c Note: The << insertion operator interprets the addresses in hexadecimal values.

Addresses and contents of variables.

Pointer Variables Pointers are variables that hold address values. The syntax used in C++ allows pointers to any type to be declared.  Examples of pointer declarations: char* cptr; // pointer to char Int * iptr; // pointer to int Float * fptr; // pointer to float char* ptr1, * ptr2, * ptr3; // three variables of type char*

A Simple Program Using Function Declaration

Accessing the Variable Pointed To Remember that the asterisk used as the dereference operator has a different meaning than the asterisk used to declare pointer variables. The dereference operator precedes the variable and means value of the variable pointed to by. The asterisk used in a declaration means pointer to. Examples: int* ptr; //declaration: pointer to int *ptr = 37; //indirection: value of variable pointed to by ptr int v; //defines variable v of type int int* p; //defines p as a pointer to int p = &v; //assigns address of variable v to pointer p v = 3; //assigns 3 to v *p = 3; //dereferencing: also assigns 3 to v

// pointers (address variables) #include int main() { int var1 = 11, var2 = 22 ; //two integer variables cout<<"\n print addresses of variables using address operator &"<<endl; cout << “\t”<<&var1 <<“\t” << &var2 << endl; cout<<"\n print addresses of variables using a pointer variable ptr "<<endl; int* ptr1; //pointer to integers ptr1 = &var1; //pointer points to var1 cout << ptr1 << endl; //print pointer value ptr = &var2; //pointer points to var2 cout << ptr << endl; //print pointer value cout<<"\n print the values of variables using a pointer variable *ptr "<<endl; ptr = &var1; //pointer points to var1 cout << *ptr << endl; //print the int value that the pointer points to ptr = &var2; //pointer points to var2 cout << *ptr << endl; //print the int value that the pointer points to return 0; }

Variable Access via pointer.

// pointers (address variables) #include int main() { int var1 = 122, var2 = 435 ; //two integer variables cout<<"\n The addresses of variables using address operator &"<<endl; cout << "\t"<<&var1 <<"\t" << &var2 << endl; cout<<"\n The addresses of variables using pointers ptr1 and ptr2 "<<endl; int* ptr1, * ptr2; //pointer to integers ptr1 = &var1; //pointer points to var1 ptr2 = &var2; //pointer points to var2 cout <<"\t"<< ptr1 <<"\t"<<ptr2 << endl; //print pointers values cout<<"\n The values of variables pointed by ptr1 and ptr2 "<<endl; cout <<"\t"<< *ptr1 <<"\t"<<*ptr2 << endl; //print pointers values return 0; }

Pointers and Arrays Array elements can be accessed using pointer notation as well as array notation. The name of an array is its starting address, for example: int intarray[5];//this means intarray has the address of that array cout<< * ( intarray+3 ); //this prints the fourth element in the array intarray The C++ compiler takes the size of the data type into account when it performs data addresses. It knows that intarray is an array of type int because it was declared that way. So when it sees the expression intarray+3, it interprets it as the address of the fourth integer in intarray, not the fourth byte.

Pointers and Arrays

` // array accessed with pointer notation #include using namespace std; int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; //array for(int j=0; j<5; j++) // for each element, cout << *(intarray+j) << endl; // print value return 0; }