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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
1 Pointers and Strings Section 5.4, , Lecture 12.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
CSC Programming for Science Lecture 30: Pointers.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
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 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
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”
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Engineering Computing I Chapter 5 Pointers and Arrays.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
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.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
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.
Computer Skills2 for Scientific Colleges
Chapter 7 - Pointers Outline 7.1 Introduction
Introduction to Programming
Programming fundamentals 2 Chapter 2:Pointer
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
C++ Pointers and Strings
C++ Programming Lecture 17 Pointers – Part I
C Programming Pointers
Chapter 9: Pointers and String
C++ Pointers and Strings
Programming fundamentals 2 Chapter 3:Pointer
Introduction to Pointers
Presentation transcript:

Introduction to Programming Lecture 15

In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers Expression Pointers Arithmetic Pointers Arithmetic Multidimensional Arrays Multidimensional Arrays Pointer String and Array Pointer String and Array

Pointers and Arrays Starting Address of Array y int y [ 10 ] ; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

The name of the array is like a pointer which contain the address of the first element.

Declaration of a Pointer Variable int y [ 10 ] ; int *yptr ; yptr = y ; yptr is a pointer to integer

Declaration of a Pointer Variable y [ 3 ] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

int y [ 10 ] ; int *yptr ; yptr = y ; yptr ++ ;

pointer variable yPtr y[0]y[1]y[2]y[4]y[3] location

In this case yptr is a pointer to integer so now when we increment yptr it points to the next integer

Example 1 #include<iostream.h> main ( ) { int y [ 10 ] ; int *yptr = y ; yptr = y ; cout << yptr ; yptr ++ ; cout << yptr ; }

yptr = y ; is same as yptr = &y [ 0 ] ; …….. yptr = &y [ 2 ] ;

Example 2 #include<iostream.h> main ( ) { int y [ 10 ] ; int *yptr ; yptr = y ; cout << yptr ; yptr ++ ; cout << *yptr ; }

main ( ) { int x = 10 ; int *yptr ; yptr = &x ; cout << yptr ; cout << *yptr ; *yptr ++ ; } Example 3 increment whatever yptr points to

Pointer Arithmetic *yptr + 3 ; cout << *yptr ; *yptr += 3 ; This Is an Expression

Pointer Arithmetic yptr = &x ; yptr ++ ;

Pointer Arithmetic int x =10 ; int *yptr ; yptr = &x ; *yptr += 3 ; yptr += 3 ;

Decrementing *yptr --

Pointer Arithmetic int *p1,*p2; ….. p1 + p2 ; Error

Pointer Arithmetic int y [ 10 ], *y1, *y2 ; y1 = &y [ 0 ] ; y1 = &y [ 0 ] ; y2 = &y [ 3 ] ; y2 = &y [ 3 ] ; cout << y2 - y1 ; cout << y2 - y1 ;

Pointer Arithmetic int y [ 10 ] ; int *yptr ; yptr = y [ 5 ] ; cout << *( yptr + 5 ) ;

Pointer Comparison if ( y1 > y2 ) if ( y1 >= y2 ) if ( y1 == y2 )

Pointer Comparison if ( *y1 > *y2 )

Example int y [ 10 ] ; int *yptr ; yptr = y ; cout << y [ 5 ] ; cout << ( yptr + 5 ) ; cout << *( yptr + 5 ) ;

Example int que [ 10 ] ; int y [ 10 ]; int *yptr ; yptr = y ; yptr = que ;

pointer variable vPtr v[0]v[1]v[2]v[4]v[3] location

Strings

String Initialization char name [ 20 ] ; name [ 0 ] = ‘A’ ; name [ 1 ] = ‘m’ ; name [ 2 ] = ‘i’ ; name [ 3 ] = ‘r’ ; name [ 4 ] = ‘\0’ ;

String Initialization Strings is always terminated with \0

String Initialization char name [ 20 ] = “Amir” ; Array must be one character space larger than the number of printable character which are to be stored. Array must be one character space larger than the number of printable character which are to be stored.

Example 4 char string1 [ 20 ] = “Amir”; char string2 [ 20 ] ; char *ptrA, *ptrB ; prtA = string1 ; prtB = string2 ; while ( *ptrA != ‘\0’ ) { *ptrB++ = *ptrA++; } *ptrB = ‘\0’ ;

String Copy Function myStringCopy ( char *destination, const char *source ) { while ( *source != ‘\0’ ) { *destination++ = *source++ ; *destination++ = *source++ ;} *destination = ‘\0’ ; }

In Today’s Lecture Pointers and Arrays Pointers and Arrays Pointer Arithmetic Pointer Arithmetic Manipulation of Arrays Manipulation of Arrays String Arrays String Arrays