C++ Pointers and Strings

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

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.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
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.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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: 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.
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.
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.
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.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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”
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
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 Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
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.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Standard Version of Starting Out with C++, 4th Edition
Fundamentals of Characters and Strings
Chapter 9: Pointers.
Motivation and Overview
Pointers.
Learning Objectives Pointers Pointer in function call
C Basics.
Lecture 6 C++ Programming
Object-Oriented Programming Using C++
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation
Chapter 9: Pointers.
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS 2308 Exam I Review.
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
CS 2308 Exam I Review.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Pointers Kingdom of Saudi Arabia
Programming in C Pointer Basics.
Chapter 16 Pointers and Arrays
C++ winter2008(by:J.Razjouyan)
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
Standard Version of Starting Out with C++, 4th Edition
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Pointers.
Presentation transcript:

C++ Pointers and Strings C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Pointers A pointer is a variable that holds the address of something else. ... MEMORY 1 2 3 4 5 81345 81346 81347 Address int foo; int *x; foo = 123; x = &foo; foo 123 x 3 C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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” C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) &foo In C++ you can get the address of a variable with the “&” operator. ... MEMORY 1 2 3 4 5 Address int foo; foo = 123; x = &foo; foo 123 &foo means “the address of foo” C++ winter2008(by:J.Razjouyan)

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; x doesn’t point to anything!!! ERROR!!! this is fine x points to foo C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Pointers to anything x some int int *x; int **y; double *z; y some *int some int z some double C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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] C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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 *ptr = a; *(ptr+2) *(ptr+4) *ptr a[0] a[1] a[2] a[3] a[4] int a[5]; C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) printing an array void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } array version void print_array(int *a, int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << *a++ << endl; } pointer version C++ winter2008(by:J.Razjouyan)

Passing pointers as parameters void swap( int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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) C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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”; zero (null) msg 'R' 'P' 'I' C++ winter2008(by:J.Razjouyan)

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! C++ winter2008(by:J.Razjouyan)

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 C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Another way int count_string( char *s) { char *ptr = s; while (*ptr) { ptr++; } return(ptr - s); pointer arithmetic! C++ winter2008(by:J.Razjouyan)

Exercises (for those so inclined) Write strcpy Write a function that compares 2 strings and returns true if they are the same string, false if they are not. Write a function that removes all spaces from a string. C++ winter2008(by:J.Razjouyan)