Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

An introduction to pointers in c
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
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.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
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.
Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different.
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Lecture No.01 Data Structures Dr. Sohail Aslam
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C is a high level language (HLL)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
EGR 2261 Unit 11 Pointers and Dynamic Variables
© 2016 Pearson Education, Ltd. All rights reserved.
Pointers in C.
Pointers Psst… over there.
Basic notes on pointers in C
References and Pointers
Pointers Psst… over there.
5.1 Introduction Pointers Powerful, but difficult to master
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C++ Pointers and Strings
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
CS250 Introduction to Computer Science II
C Programming Pointers
Chapter 9: Pointers and String
C++ Pointers and Strings
POINTER CONCEPT 8/3/2019.
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Introduction to C CS 3410.
Presentation transcript:

Pointers 1

Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered

Pointers in C++ is a very powerful programming technique. It is very useful in: ◦ To call functions using “Pass-by-Reference” technique ◦ Dynamic memory ◦ Improve the efficiency of functions ◦ Develop dynamic data structures, such as binary trees and linked lists etc. 3 Introduction

A pointer is a variable that holds a memory address. This address is the location of another variable in memory. 4 Introduction contd. 10 int count 0X10012 Variable Name Memory Address Value Stored at the Memory address 0X10012 int *count_ptr 0X10014 A normal variable A pointer variable

In spite of the powerfulness pointers are very dangerous as well. ◦ Programs with pointers can go wrong very easily. ◦ Programmers often make lot of mistakes in manipulating pointers. Due to this reason new languages such as Java had eliminated ‘Pointer’ concept altogether. 5 Introduction contd.

Like the normal variables, pointer variables must be declared before use. Pointers are denoted using the sign * Examples: ◦ int *count_ptr – A pointer to an integer variable ◦ float *avg_ptr – A pointer to a floating point variable ◦ char *choice_ptr - A pointer to a character variable 6 Declaring a Pointer Variable

Initially when you declare a pointer variable it will be NULL ◦ The pointer dose not point to any object. 7 Declaring a Pointer Variable contd. NULL int *count_ptr 0X10014 Variable Name Memory Address Value Stored at the Memory address

There are two types of pointer operators. 8 Pointer Operators OperatorMeaningDescription &Address of operator Returns the memory address of a variable *Dereferencing operator Returns the value stored at the location pointed by a pointer

Example 1.int marks; 2.int *marks_ptr; 3.marks = 30; 4.marks_ptr = &marks; We can use & operator to initialize a pointer variable 9 The Address of Operator - & int marks 0X10034 int *marks_ptr 0X X

Example 1.int marks, value; 2.int *marks_ptr; 3.marks = 30; 4.marks_ptr = &marks; 5.value = *marks_ptr; 10 The Dereferencing Operator - * int marks 0X10034 int *marks_ptr 0X X int value 0X

The Dereferencing Operator is useful when we want to access and modify the actual content of the variable which is pointed by a pointer. 11 The Dereferencing Operator contd.

What is the result of the following code ? int number; int *number_ptr; number = 30; number_ptr = &number; *number_ptr = 50; cout<<“number = ”<<number”; 12 Pointer Operators - Exercise number = 50

What is the result of the following code ? int number; int *number1_ptr; int *number2_ptr; number = 30; number1_ptr = &number; number2_ptr = number1_ptr; *number2_ptr = 10; cout<<"number =“<<number; 13 Pointer Operators - Exercise number = 10

What is the result of the following code ? int number1 = 10; int number2 = 20; int *number1_ptr; int *number2_ptr; number1_ptr = &number1; number2_ptr = &number2; *number2_ptr = *number1_ptr; * number1_ptr = 50; cout<<"number1 = “<<number1; cout<<”number2 = “<<number2; 14 Pointer Operators - Exercise number1 = 50 number2 = 10

Thank you. 15