Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.

Slides:



Advertisements
Similar presentations
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
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 and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © 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.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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 Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 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 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
PGT 106 C Programming POINTERS. PGT 106 C Programming Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
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)
8 Pointers.
Chapter 7 from “C How to Program"
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
7 C Pointers.
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
POINTERS.
C++ Programming Lecture 18 Pointers – Part II
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Lecture 9 - Pointers 1

Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer Expressions and Pointer Arithmetic Relationship between Pointers and Arrays Arrays of Pointers 2

Introduction Pointer is the address (i.e. a specific memory location) of an object. It can refer to different objects at different times. Pointers are used in C programs for a variety of purposes: To return more than one value from a function (using pass by reference) To create and process strings To manipulate the contents of arrays and structures To construct data structures whose size can grow or shrink dynamically 3

Pointer Variable Definitions and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointer contains an address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value num 7 7 numPtr 4

Pointer Variable Definitions and Initialization Pointer definitions * is used with pointer variables int *numPtr; Defines a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable definition int *numPtr1, *numPtr2; Can define pointers to any data type Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred) int *numPtr = NULL; or int *numPtr = 0; 5

Pointer Operators Symbol & is called address operator Returns address of operand int num = 7; int *numPtr; numPtr = # /* numPtr gets address of num */ numPtr “points to” num numPtr num 7 numPtr num Address of num is value of numPtr 6

Pointer Operators Symbol * is called indirection/dereferencing operator Returns a synonym/alias of what its operand points to *numPtr returns num (because numPtr points to num ) * can also be used for assignment Returns alias to an object *numPtr = 10; /* changes num to 10 */ show pictures!! Dereferenced pointer (operand of * ) must be an lvalue (no constants) * and & are inverses They cancel each other out 7

#include int main() { int num; int *numPtr; int num1=5; num = 7; printf("number = %d\n", num); numPtr = # printf("numPtr points to num whereby the value is = %d\n",*numPtr); printf("Address of numPtr : %d Contents of numPtr : %d\n", &numPtr, numPtr); printf("Address of num : %d\n\n", &num); *numPtr = 15; printf("Dereferencing pointer, *numPtr = %d\n", *numPtr); num = num + num1; printf(“num = %d\n”, num); printf("*numPtr = %d\n", *numPtr); printf("*numPtr + num1 = %d\n", *numPtr + num1); return 0; } Sample program number = 7 numPtr points to num whereby the value is = 7 Address of numPtr : Contents of numPtr : Address of num : Dereferencing pointer, *numPtr = 15 num = 20 *numPtr = 20 *numPtr + num1 = 25 8

Calling Functions by Reference Call by reference with pointer arguments Passes address of argument using & operator Allows you to change actual location in memory Arrays are not passed with ‘ & ’ because the array name is already a pointer * operator Used as alias or nickname for variable inside of function void fun1 (int *number) { *number = 2 * (*number); } *number used as nickname for the variable passed 9

Remember..last time #include char read(); void find_count_vc(char, int*, int*); void print(int,int); int main() { char ch, choice; int count_v=0,count_c=0; do { ch = read(); find_count_vc(ch, &count_v, &count_c); printf("Do you want to continue?"); scanf("%c", &choice); getchar(); }while((choice == 'y') ||(choice =='Y')); print(count_v,count_c); return 0; } char read() { char ch1; printf("Enter character : "); scanf("%c", &ch1); getchar(); return(ch1); } void find_count_vc(char ch1, int *vowel, int *consonant) { switch(ch1) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': *vowel = *vowel +1;break; default: *consonant = *consonant + 1; } void print(int vowel, int consonant) { printf("Number of vowel : %d\n", vowel); printf("Number of consonant : %d\n", consonant); } Enter character : f Do you want to continue?y Enter character : I Do you want to continue?y Enter character : k Do you want to continue?n Number of vowel : 1 Number of consonant : 2 Functions that “return” more than one value i.e. arguments are passed by ref 10

Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers Increment/decrement pointer ( ++ or -- ) Add an integer to a pointer ( + or +=, - or -= ) Pointers may be subtracted from each other Operations meaningless unless performed on an array 11

Pointer Expressions and Pointer Arithmetic 5 element int array on machine with 4 byte ints vPtr points to first element v[ 0 ] at location 3000 (vPtr = 3000) vPtr += 2; sets vPtr to 3008 vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to address 3008 pointer variable vPtr v[0]v[1]v[2]v[4]v[3] location

Pointer Expressions and Pointer Arithmetic Subtracting pointers Returns number of elements from one to the other. If vPtr2 = &v[ 2 ]; vPtr = &v[ 0 ]; vPtr2 - vPtr would produce 2 Pointer comparison ( ) See which pointer points to the higher numbered array element Also, see if a pointer points to 0 13

Example of Pointer Operations #include int main() {int *vPtr; int *vPtr2; int v[5] = {10,20,30,40,50}; int temp; int *p, *q; vPtr= v; printf("Address of vPtr : %d Contents of vPtr : %d\n", &vPtr, vPtr); printf("Address of v[0] : %d\n", &v); vPtr +=2; printf("Address of vPtr + 2: %d\n", vPtr); vPtr +=2; printf("Address of vPtr + 4: %d\n", vPtr); vPtr2=&v[2]; vPtr=&v[0]; temp=vPtr2-vPtr; printf("Contents of temp : %d\n", temp); p=q; printf("Contents of p : %d q: %d\n", p,q); return 0;} Address of vPtr : Contents of vPtr : Address of v[0] : Address of vPtr + 2: Address of vPtr + 4: Contents of temp : 2 Contents of p : q:

The Relationship between Pointers and Arrays Arrays and pointers are closely related Array name like a constant pointer Pointers can do array subscripting operations Define an array b[5] and a pointer bPtr To set them equal to one another use: bPtr = b; The array name ( b ) is actually the address of first element of the array b[5] bPtr = &b[0]; Explicitly assigns bPtr to the address of first element of b 15

The Relationship between Pointers and Arrays Element b[3] Can be accessed by *(bPtr + 3) where * is the offset. Called pointer/offset notation Can be accessed by bPtr[3] Called pointer/subscript notation bPtr[3] same as b[3] Can be accessed by performing pointer arithmetic on the array itself *(b + 3) 16

Example Address of bPtr : Contents of bPtr : Address of b : Contents of b[0]: bPtr points to b[0] = 10 I am accessing element b[3]!! Let see how many ways I can do it b[3] = 40 *(bPtr + 3) = 40 *(b + 3) = 40 bPtr[3] = 40 b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 b[4] = 50 b[5] = 0 b[6] = 0 b[7] = 0 b[8] = 0 b[9] = 0 #include int main() { int *bPtr ;int i; int b[10]={10,20,30,40,50}; bPtr = b; printf("Address of bPtr : %d Contents of bPtr : %d\n", &bPtr, bPtr); printf("Address of b : %d Contents of b[0]:%d %d %d\n", &b, b[0], *bPtr, *b); printf("bPtr points to b[0] = %d\n", *bPtr); printf("\nI am accessing element b[3]!!\nLet see how many ways I can do it\n"); printf("b[3] = %d\n", b[3]); printf("*(bPtr + 3) = %d\n", *(bPtr + 3)); printf("*(b + 3) = %d\n", *(b + 3)); printf("bPtr[3] = %d\n\n", bPtr[3]); for(i=0;i<10;i++) printf("b[%d] = %d\n", i, *(bPtr+i)); return 0; } 17

Arrays of Pointers Arrays can contain pointers For example: an array of strings char *suit[4] = {“Hearts”,“Diamonds”,“Clubs”,“Spades”}; Strings are pointers to the first character char * – each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored 18

Arrays of Pointers suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’ 19

Example #include #define N 5 int main() { char *studentName[N]; int i; for(i=0;i<5;i++) { printf("Enter student[%d] name : ", i); scanf("%s", studentName + i); printf("You just entered :\n%s\n", studentName + i); } return 0; } Enter student[0] name : ali You just entered : ali Enter student[1] name : abu You just entered : abu Enter student[2] name : cheah You just entered : cheah Enter student[3] name : dali You just entered : dali Enter student[4] name : gheeta You just entered : gheeta 20