Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
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.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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 Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
CS1010 Programming Methodology
CS1010 Programming Methodology
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
INC 161 , CPE 100 Computer Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers Psst… over there.
8 Pointers.
Chapter 7 from “C How to Program"
Pointers.
Pointers Psst… over there.
Lecture 18: The Elegant, Abstract World of Computing
Pointers and Pointer-Based Strings
Programming with Pointers
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
POINTERS.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
C Pointers Another ref:
Presentation transcript:

Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address of a variable that refers to a specific value. A variable directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection.

Operators & ‘address of’ returns the address of an object or variable * ‘value of’ refers to the value at a given address

Directly and Indirectly referencing a variable int Sum = 5; int X = 3; int* z = &X; Address Name Value 100011 Sum 5 100100 X 3 z=100100, *z=3, &Sum=100011

Initialization A pointer may be initialized to 0 , NULL or an address. NULL is a symbolic constant defined in <stdio.h> A pointer with the value NULL points to nothing. The only integer that should be assigned to a pointer is 0. Dereferencing The * operator – indirection or dereferencing operator, returns the value of the object that its operand points to in memory. This is called dereferencing the pointer.

/* Program to display the address and value of a pointer */ #include <stdio.h> int main() { int a; int *aPtr; a = 200; aPtr = &a; printf(“The address of a is %p \n”, &a); printf(“The value of aPtr is %p \n”, aPtr); printf(“The value of a is %d \n”, a); printf(“The value of *aPtr is %d \n”, *aPtr); }

Calling functions by reference #include <stdio.h> void cubebyref( int *); int main() { int num = 5; printf(“The number is %d \n”,num); cubeByRef(&num); printf(“The new value of number is %d \n”,num); return 0; } void cubeByRef(int *numPtr) *numPtr = *numPtr * *numPtr * *numPtr ;

Relationship between Pointers and Arrays An array name is the address of the first element of an array. int b[5]; The address of the first element : b, &b[0] Assume that int b[5] and pointer variable bPtr have been declared. Since the array name is a pointer to the first element of the array: int* bPtr = b; is equivalent to int* bPtr = &b[0]; Array element b[1] can be referenced by: *( bPtr + 1) 1 is the ‘offset’ with respect to the pointer. This notation is called pointer / offset notation.

In pointer/offset notation, the offset is the same as the array subscript. Pointers can be subscripted exactly as arrays can. bPtr[1] refers to the array element b[1]. This is called pointer / subscript notation.

#include <stdio.h> int main() { int b[] = { 10 , 20 , 30 , 40 }; int *bPtr = b; int i, offset; printf(“Pointer/offset notation where the pointer is ”); printf(“the array name \n”); for(offset = 0; offset < 4; offset++) printf(“*(b + %d) = %d \n”, offset, *( b + offset) ); printf(“Pointer/subscript notation \n”); for( i = 0 ; i < 4 ; i++) printf(“bPtr[ %d ] = %d \n”, i, bPtr [i ]); printf(“pointer/offset notation \n”); for (offset = 0; offset < 4 ; offset ++) printf(“ *(bPtr + %d) = %d \n”, offset,*(bPtr + offset)); return 0; }

#include <stdio.h> void copy( char *, const char *); int main() { char string1[10], *string2 = “CS230”; char string3[10], string4[ ] = “Lab”; copy(string1, string2); printf(“string1 = %s \n”,string1); copy(string3,string4); printf(“string3 = %s \n”,string3); return 0; }

void copy(char *s1, const char *s2) { int i = 0; do s1[i] = s2[i]; i++; } while (s2[i] != ‘\0’); s1[i] = ‘\0’;