Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Pointers. andy = 25; fred = andy; ted = &andy; andy = 25; ted = &andy; beth = *ted;
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 CSE1301 Computer Programming Lecture 16 Pointers.
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.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Science 210 Computer Organization Pointers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
First steps Jordi Cortadella Department of Computer Science.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
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.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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.
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?
Introduction to Programming Using C
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
CSC 172 DATA STRUCTURES.
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointer Basics Psst… over there.
Pointers & Functions.
Local Variables, Global Variables and Variable Scope
Introduction to C++ Programming Language
Pointers Lecture 1 Thu, Jan 15, 2004.
Pointers and Pointer-Based Strings
C Programming Lecture 0 : Introduction
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Pointers & Functions.
Pointer Data Type and Pointer Variables
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
CS148 Introduction to Programming II
Introduction to Pointers
Presentation transcript:

Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

Pointer Pointers Variables that contain memory addresses Pointer Operators * (dereference operator) : Unary * takes the contents of a pointer (dereferences a pointer) & (reference operator) : Unary & gives the address of (pointer to) something Declaring variables of pointer types Declare which data type a pointer is going to point to (ex) int* a; // ‘a’ is a variable that stores an address of // an integer type variable. float* b; void swap(int* x, int* y); NULL Pointer?

C++ example 1 // more pointers #include using namespace std; int main () { int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; // p1 = address of firstvalue p2 = &secondvalue; // p2 = address of secondvalue *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0; } Output : firstvalue is 10 secondvalue is 20

Pointers and Arrays Array variable is a constant pointer the identifier of an array is equivalent to the address of its first element (ex) int numbers[20]; int *p; p=numbers; // OK numbers=p; // NOT OK! numbers[5]=0; // numbers [offset of 5] = 0; *(numbers+5)=0; // value pointed by (numbers+5) = 0; *p=1; p++; *p=2;

C++ example 2 // more pointers #include using namespace std; int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0; } Output : 10, 20, 30, 40, 50,

Pointers to Pointers C++ allows the use of pointers that point to pointers char a; char * b; char ** c; a = 'z'; b = &a; c = &b; c has type char** and a value of 8092 *c has type char* and a value of 7230 **c has type char and a value of 'z'