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

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

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,
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
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.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
 2006 Pearson Education, Inc. All rights reserved Pointers.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
 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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 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.
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.
Lecture 17: The Last Puzzle Piece with Functions.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
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.
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 7 from “C How to Program"
Simple Data Types and Function Calls
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
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.
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

BBS514 Structured Programming (Yapısal Programlama)1 Pointers

A pointer is a variable that contains the address of a variable. Pointers provide a powerful and flexible method for manipulating data in your programs; but they are difficult to master. Pointers allow you to reference a large data structure in a compact way. Pointers facilitate sharing data between different parts of a program. –Call-by-Reference Dynamic memory allocation: Pointers make it possible to reserve new memory during program execution. BBS514 Structured Programming (Yapısal Programlama)2

Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as their values –Normal variables contain a specific value (direct reference) –Pointers contain address of a variable that has a specific value (indirect reference) –Indirection – referencing a pointer value BBS514 Structured Programming (Yapısal Programlama)3 count 7 7 countPtr

Pointer Variable Declarations and Initialization BBS514 Structured Programming (Yapısal Programlama)4 Pointer declarations –A pointer declaration takes the following form: type*identifier; e.g. int *myPtr; –Declares a pointer to an int (pointer of type int * ) –Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2; –Can declare pointers to any data type –Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred)

Pointer Operators BBS514 Structured Programming (Yapısal Programlama)5 & (address operator) –Returns the address of operand int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y –yPtr “points to” y yPtr y 5 yptr y 5 Address of y is value of yptr

Pointer Operators BBS514 Structured Programming (Yapısal Programlama)6 * (indirection/dereferencing operator) –Returns a synonym/alias of what its operand points to –*yptr returns y (because yptr points to y ) –* can be used for assignment Returns alias to an object *yptr = 7; // changes y to 7 * and & are inverses –They cancel each other out

BBS514 Structured Programming (Yapısal Programlama)7 int rate; int *p_rate; rate = 500; p_rate = &rate; rate p_rate Memory /* Print the values */ printf(“rate = %d\n”, rate); /* direct access */ printf(“rate = %d\n”, *p_rate); /* indirect access */

BBS514 Structured Programming (Yapısal Programlama)8 Program Output 1/* Using the & and * operators */ 2 3#include 4 5int main() 6{6{ 7 int a; /* a is an integer */ 8 int *aPtr; /* aPtr is a pointer to an integer */ 9 10 a = 7; 11 aPtr = &a; /* aPtr set to address of a */ printf( "The address of a is %p" 14 "\nThe value of aPtr is %p", &a, aPtr ); printf( "\n\nThe value of a is %d" 17 "\nThe value of *aPtr is %d", a, *aPtr ); printf( "\n\nShowing that * and & are inverses of " 20 "each other.\n&*aPtr = %p" 21 "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; 24} The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88 The address of a is the value of aPtr. The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a. Notice how * and & are inverses

Addressing and Dereferencing BBS514 Structured Programming (Yapısal Programlama)9 int a, b, *p; a = b = 7; p = &a; printf(“*p = %d\n”,*p); *p = 3; printf(“a = %d\n”,a); p = &b; *p = 2 * *p - a; printf(“b = %d \n”, b);

Addressing and Dereferencing BBS514 Structured Programming (Yapısal Programlama)10 float x, y, *p; x = 5; y = 7; p = &x; y = *p; Thus, y = *p; y = *&x; y = x; All equivalent

Addressing and Dereferencing BBS514 Structured Programming (Yapısal Programlama)11

Pointers to void BBS514 Structured Programming (Yapısal Programlama)12

Calling Functions by Reference BBS514 Structured Programming (Yapısal Programlama)13 Call by reference with pointer arguments –Pass address of argument using & operator –Allows you to change actual location in memory * operator –Used as alias/nickname for variable inside of function void double_it( int *number ) { *number = 2 * ( *number ); } – *number used as nickname for the variable passed

Passing parameters by reference BBS514 Structured Programming (Yapısal Programlama)14 void SetToZero (int var) { var = 0; } You would make the following call: SetToZero(x); This function has no effect whatever. Instead, pass a pointer: void SetToZero (int *ip) { *ip = 0; } You would make the following call: SetToZero(&x); This is referred to as call-by-reference.

BBS514 Structured Programming (Yapısal Programlama)15 /* An example using call-by-reference */ #include void change_arg(int *y); int main (void) { int x = 5; change_arg(&x); printf(“%d \n”, x); return 0; } void change_arg(int *y) { *y = *y + 2; }

BBS514 Structured Programming (Yapısal Programlama)16 Program Output 1/* Cube a variable using call-by-reference 2 with a pointer argument */ 3 4 5#include 6 7void cubeByReference( int * ); /* prototype */ 8 9int main() 10{ 11 int number = 5; printf( "The original value of number is %d", number ); 14 cubeByReference( &number ); 15 printf( "\nThe new value of number is %d\n", number ); return 0; 18} 19 20void cubeByReference( int *nPtr ) 21{ 22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */ 23} The original value of number is 5 The new value of number is 125 Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable). Inside cubeByReference, *nPtr is used ( *nPtr is number ). Notice that the function prototype takes a pointer to an integer ( int * ).

BBS514 Structured Programming (Yapısal Programlama)17 /* Cube a variable using call by value */ #include int CubeByValue (int n); int main(void) { int number = 5; printf(“The original value of number is %d\n”, number); number = CubeByValue(number); printf(“The new value of number is %d\n”,number); return 0; } int CubeByValue (int n) { return (n*n*n); }

BBS514 Structured Programming (Yapısal Programlama)18 /* Swapping arguments (incorrect version) */ #include void swap (int p, int q); int main (void) { int a = 3; int b = 7; printf(“%d %d\n”, a,b); swap(a,b); printf(“%d %d\n”, a, b); return 0; } void swap (int p, int q) { int tmp; tmp = p; p = q; q = tmp; }

BBS514 Structured Programming (Yapısal Programlama)19 /* Swapping arguments (correct version) */ #include void swap (int *p, int *q); int main (void) { int a = 3; int b = 7; printf(“%d %d\n”, a,b); swap(&a, &b); printf(“%d %d\n”, a, b); return 0; } void swap (int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } pq p q

BBS514 Structured Programming (Yapısal Programlama)20 /* * This function separates a number into three parts: a sign (+, -, * or blank), a whole number magnitude and a fraction part. * Preconditions: num is defined; signp, wholep and fracp contain * addresses of memory cells where results are to be stored * Postconditions: function results are stored in cells pointed to by * signp, wholep, and fracp */ void separate(double num, char *signp, int *wholep, double *fracp) { double magnitude; if (num < 0) *signp = ‘-‘; else if (num == 0) *signp = ‘ ‘; else *signp = ‘+’; magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; }

BBS514 Structured Programming (Yapısal Programlama)21 int main() { double value; char sn; int whl; double fr; // Gets data printf(“Enter a value to analyze:”); scanf(“%lf”, &value); // Separates data value in three parts separate(value, &sn, &whl, &fr); //Prints results printf(“Parts of %.4f\n sign: %c\n”, value, sn); printf(“whole number magnitude: %d\n”, whl); printf(“fractional part : %.4f\n”, fr); return 0; }

Pointers and Arrays BBS514 Structured Programming (Yapısal Programlama)22 Arrays are implemented as pointers. Consider: double list[3]; &list[1] : is the address of the second element &list[i] : the address of list[i] which is calculated by the formula base address of the array + i * 8

The Relationship Between Pointers and Arrays BBS514 Structured Programming (Yapısal Programlama)23 Arrays and pointers are closely related –Array name is like a constant pointer –Pointers can do array subscripting operations Declare an array b 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 bPtr = &b[ 0 ] Explicitly assigns bPtr to address of first element of b

The Relationship Between Pointers and Arrays BBS514 Structured Programming (Yapısal Programlama)24 –Element b[ 3 ] Can be accessed by *( bPtr + 3 ) –Where n 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 )

Example BBS514 Structured Programming (Yapısal Programlama)25 /* Using subscripting and pointer notations with arrays */ #include int main(void) { int i, offset, b[4]={10,20,30,40}; int *bPtr = b; /* Array is printed with array subscript notation */ for (i=0; i < 4; i++) printf(“b[%d] = %d\n”, i, b[i]);

Example (cont.) BBS514 Structured Programming (Yapısal Programlama)26 /* Pointer/offset notation where the pointer is the array name */ for (offset=0; offset < 4; offset++) printf(“*(b + %d) = %d\n”,offset,*(b + offset)); /* Pointer subscript notation */ for (i=0; i < 4; i++) printf(“bPtr[%d] = %d\n”, i, bPtr[i]); /* Pointer offset notation */ for (offset = 0; offset < 4; offset++) printf(“*(bPtr + %d) = %d\n”, offset” “*(bPtr + offset)”); return 0; }

Pointer Arithmetic BBS514 Structured Programming (Yapısal Programlama)27 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

Pointer Expressions and Pointer Arithmetic BBS514 Structured Programming (Yapısal Programlama)28 5 element int array on machine with 4 byte int s –vPtr points to first element v[ 0 ] i.e. location 3000 ( vPtr = 3000 ) –vPtr += 2; sets vPtr to 3008 vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte int s, 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 BBS514 Structured Programming (Yapısal Programlama)29 Subtracting pointers –Returns number of elements from one to the other. If vPtr = &v[ 0 ]; vPtr2 = &v[ 2 ]; //vPtr2 = vPtr + 2; –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

Pointer Expressions and Pointer Arithmetic BBS514 Structured Programming (Yapısal Programlama)30 Pointers of the same type can be assigned to each other –If not the same type, a cast operator must be used –Exception: pointer to void (type void * ) Generic pointer, represents any type No casting needed to convert a pointer to void pointer void pointers cannot be dereferenced

BBS514 Structured Programming (Yapısal Programlama)31 Pointers var. name a b c d address int a=10; int *b; int **c; int d[3]={1,2,3}; b=&a; *b=5; c=&b; *(*c)=8; b=d; *(*c)=9; *(*c+2)=15;

Example BBS514 Structured Programming (Yapısal Programlama)32 int SumIntegerArray(int *ip, int n) { int i, sum; sum = 0; for (i=0; i < n; i++) { sum += *ip++; } return sum; } Assume int sum, list[5]; are declared in the main function. We can make the following function call: sum = SumIntegerArray(list, 5);