Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
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 A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
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.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
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. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
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.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
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.
Lecture 10 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
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.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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”
Review 1 List Data Structure List operations List Implementation Array Linked List.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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. 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 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.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Pointers and Dynamic Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Chapter 9: Pointers.
Learning Objectives Pointers Pointer in function call
Lecture 6 C++ Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 9: Pointers.
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
CS 2308 Exam I Review.
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Pointers Lecture 1 Thu, Jan 15, 2004.
C++ Pointers and Strings
C++ winter2008(by:J.Razjouyan)
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Pointers.
Presentation transcript:

Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea

C++ Pointers and Strings

10/2014C++ ProgrammingL6.3L6.3 Pointers A pointer is a variable that holds the address of something else.... MEMORY Address int foo; int *x; foo = 123; x = &foo; foo x 123 3

10/2014C++ ProgrammingL6.4L6.4 int *x; x is a pointer to an integer. You can use the integer x points to in a C++ expression like this: y = *x + 17; *x = *x +1; “the int x points to”

10/2014C++ ProgrammingL6.5L6.5 &foo In C++ you can get the address of a variable with the “&” operator.... MEMORY Address &foo means “the address of foo” foo int foo; foo = 123; x = &foo; 123

10/2014C++ ProgrammingL6.6L6.6 Assigning a value to a dereferenced pointer A pointer must have a value before you can dereference it (follow the pointer). int *x; *x=3; int foo; int *x; x = &foo; *x=3; ERROR!!! x doesn’t point to anything!!! this is fine x points to foo

10/2014C++ ProgrammingL6.7L6.7 Pointers to anything int *x; int **y; double *z; x some int some *int some inty z some double

10/2014C++ ProgrammingL6.8L6.8 Pointers and Arrays An array name is basically a const pointer. You can use the [] operator with a pointer: int *x; int a[10]; x = &a[2]; for (int i=0;i<3;i++) x[i]++; x is “the address of a[2] ” x[i] is the same as a[i+2]

10/2014C++ ProgrammingL6.9L6.9 Pointer arithmetic Integer math operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. int a[5]; a[0] a[1] a[2] a[3] a[4] int *ptr = a; *ptr *(ptr+2) *(ptr+4)

10/2014C++ ProgrammingL6.10 printing an array void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } void print_array(int *a, int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << *a++ << endl; } pointer version array version

10/2014C++ ProgrammingL6.11 Arrays of Pointers Like for any other type you can create arrays of pointers: int a[] = {0, 1, 2}; int b[] = {4, 5, 6}; int* x[2]; x[0] = a; x[1] = b; std::cout<<*(x[1]+1); std::cout<<*(*(x+1)+1); x is array of pointers both statements are identical

10/2014C++ ProgrammingL6.12 Passing pointers as parameters void swap(int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; }

10/2014C++ ProgrammingL6.13 Pointer Parameters Pointers are passed by value (the value of a pointer is the address it holds). If we change what the pointer points to the caller will see the change. If we change the pointer itself, the caller won't see the change (we get a copy of the pointer)

10/2014C++ ProgrammingL6.14 C++ strings A string is a null terminated array of characters. –null terminated means there is a character at the end of the the array that has the value 0 (null). Pointers are often used with strings: char *msg = "RPI"; 'R' msg zero (null) 'P''I'0

10/2014C++ ProgrammingL6.15 String Manipulation Functions C++ includes a library of string handling functions: char * strcpy(char *dst, const char *src) char * strcat(char *dst, const char *src) lots more!

10/2014C++ ProgrammingL6.16 String Example - Count the chars int count_string( char *s) { int n=0; while (*s) { n++; s++; } return(n); } while the thing pointed to by s is not null increment count set s to point to the next char

10/2014C++ ProgrammingL6.17 Another way int count_string(char *s) { char *ptr = s; while (*ptr) { ptr++; } return(ptr - s); } pointer arithmetic!

10/2014C++ ProgrammingL6.18 “void” pointers in C and C++ The following code triggers an error message in C++ but not in C void* fun () { return NULL; } void main() { int *p; p = fun(); }