Pointers and Pointer-Based Strings

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

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.
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. 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 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.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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 © – Curt Hill Pointers A Light Introduction.
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)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
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.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 7 Pointers and C-Strings
UNIT 5 C Pointers.
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Chapter 15: Overloading and Templates
Student Book An Introduction
Pointer Data Type and Pointer Variables II
Pointers Revisited What is variable address, name, value?
CSC113: Computer Programming (Theory = 03, Lab = 01)
Passing Arguments to a Function
Pointer.
Lecture 6 C++ Programming
Object-Oriented Programming Using C++
void Pointers Lesson xx
Dynamic Memory Allocation
Chapter 9: Pointers.
Object Oriented Programming COP3330 / CGS5409
Extra.
7 Arrays.
Chapter 9: Pointers.
Pointers Call-by-Reference CSCI 230
Chapter 4 Implementing Free Functions
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
Simulating Reference Parameters in C
7 Arrays.
Overloading functions
CHAPTER 2 Arrays and Vectors.
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
CHAPTER 2 Arrays and Vectors.
C++ Programming Lecture 18 Pointers – Part II
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
CISC181 Introduction to Computer Science Dr
Introduction to Pointers
Presentation transcript:

Pointers and Pointer-Based Strings CHAPTER 3 Pointers and Pointer-Based Strings

Introduction to Pointers Pointers are the powerful feature of C++ programming  . To understand pointers, you should have the knowledge of address in computer memory Computer memory is broken down into bytes and each byte has its own address . For example: In 1KB memory, there are 1024 bytes and each byte is given an address (0 - 1023).

Introduction to Pointers Pointer variables contain memory addresses as their values. Normally, a variable directly contains a specific value. However, a pointer contains the memory address of a variable that in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value.

Pointer Operators The address operator (&) is a unary operator that returns the memory address of its operand. The & operator can find address occupied by a variable. If var is a variable then, &var gives the address of that variable.

Pointer Variables Pointer variables or simply pointers are the special types of variable that holds memory address instead of data. How to declare a pointer? int *p; The statement above defines a pointer variable p. The pointer p holds the memory address. The asterisk is a dereference operator which means pointer to.  Here pointer p is a pointer to int, that is, it is pointing an integer.

Example Program #include <iostream> using namespace std; int main() { int *pc, c; c = 5; cout<< "Address of c (&c): " << &c << endl; cout<< "Value of c (c): " << c << endl << endl; pc = &c; // Pointer pc holds the memory address of variable c cout<< "Address that pointer pc holds (pc): "<< pc << endl; cout<< "Content of the address pointer pc holds (*pc): " << *pc << endl << endl; return 0; }

Output: Address of c (&c): 0x7fff5fbff80c Value of c (c): 5 Address that pointer pc holds (pc): 0x7fff5fbff80c Content of the address pointer pc holds (*pc): 5

Passing arguments to a function There are 3 ways in C++ to pass arguments to a function: Pass-by-value Pass-by-reference with reference arguments Pass-by-reference with pointer arguments Note: Pass-by-value and Pass-by-reference with reference arguments we already discussed in chapter 2.

Pass-by-reference with pointer arguments There is another way of passing argument in which actual value is not passed, only the reference to that value is passed. This method is called pass-by-reference with pointer arguments.

Example Program #include <iostream> using namespace std; int main() { void swap(int*, int*); // Function prototype int a = 1, b = 2; cout << "Before swaping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(&a, &b);

Example Program Before swapping a = 1 b = 2 After swapping a = 2 b = 1 cout << "\nAfter swaping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void swap(int* n1, int* n2) { int temp; temp = *n1; *n1 = *n2; *n2 = temp; Output: Before swapping a = 1 b = 2 After swapping a = 2 b = 1

Program Explanation In this , the address of variable is passed during function call rather than variable itself. swap(&a, &b); // &a is address of a and &b is address of b Since the address is passed instead of value, dereference operator must be used to access the value stored in that address. void swap(int* n1, int* n2) { ... .. ... } The *n1 and *n2 gives the value stored at address n1 and n2 respectively.

Using const with Pointers  The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified. If a value does not change in the body of a function to which it is passed, the parameter should be declared const to ensure that it is not accidentally modified. If an attempt is made to modify a const value, a warning or an error is issued, depending on the particular compiler.

Using const with Pointers cont..  There are 4 ways to pass a pointer to a function: A non-constant pointer to non-constant data A non-constant pointer to constant data A constant pointer to non-constant data A constant pointer to constant data

sizeof operator The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The syntax of using sizeof is as follows: sizeof (data type) Where data type is the desired data type including classes, structures, unions and any other user defined data type.

Relationship between Pointer & Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. Consider the following program: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0;

Pointers and Arrays Pointers are the variables that hold address. Pointers can point at cells of an array not only single variable. Consider this example: int* ptr; int a[5]; ptr = &a[2]; // &a[2] is the address of third element of a[5].

Pointers and Arrays You know that, pointer ptr holds the address and expression *ptr gives the content in that address. Similarly, *(ptr + 1) gives the content in address ptr + 1. Consider this code below: int p[5] = {3, 4, 5, 5, 3}; &p[0] is equal to p and *p is equal to p[0] &p[1] is equal to p+1 and *(p+1) is equal to p[1] &p[2] is equal to p+2 and *(p+2) is equal to p[2] &p[i] is equal to p+i and *(p+i) is equal to p[i]