PGT 106 C Programming POINTERS. PGT 106 C Programming Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions.

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.
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.
 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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 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.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
 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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
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. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 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, ,
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.
 2006 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
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++ 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.
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
Chapter 7 - Pointers Outline 7.1 Introduction
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.
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:

PGT 106 C Programming POINTERS

PGT 106 C Programming Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Using the const Qualifier with Pointers Pointer Expressions and Pointer Arithmetic Relationship between Pointers and Arrays Arrays of Pointers

PGT 106 C Programming 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

PGT 106 C Programming 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 iNum 7 7 piNum

PGT 106 C Programming Pointer Variable Definitions and Initialization Pointer definitions * used with pointer variables int *piNum; Defines a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable definition int *piNum1, *piNum2; Can define pointers to any data type Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred) int *piNum = NULL; or int *piNum = 0;

PGT 106 C Programming Pointer Operators Symbol & is called address operator Returns address of operand int iNum = 7; int *piNum; piNum = &iNum; /* piNum gets address of iNum */ piNum “points to” iNum piNum iNum 7 piNum iNum Address of iN um is value of piN um

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

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

PGT 106 C Programming 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 fnFun1 (int *piNumber) { *piNumber = 2 * (*piNumber); } *piNumber used as nickname for the variable passed

10 Remember..last time #include char fnRead(); void fnFindCountVC(char, int*, int*); void fnPrint(int,int); int main() { char cCh, cChoice; int iCountV=0, iCountC=0; do { cCh = fnRead(); fnFindCountVC(cCh, &iCountV, &iCountC); printf("Do you want to continue? "); scanf("%c", &cChoice); getchar(); }while((cChoice == 'y') ||(cChoice =='Y')); fnPrint(iCountV,iCountC); return 0; } char fnRead() { char cCh1; printf("Enter character : "); scanf("%c", &cCh1); getchar(); return(cCh1); } void fnFindCountVC(char cCh1, int *piVowel, int *piConsonant) { switch(cCh1) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': *piVowel = *piVowel +1;break; default: *piConsonant = *piConsonant + 1; } void fnPrint(int iVowel, int iConsonant) { printf("Number of vowel : %d\n", iVowel); printf("Number of consonant : %d\n", iConsonant); } 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 reference

PGT 106 C Programming Remember…last time #include const int iArraySize = 10; void fnInitializeArray (int aiX[], int iSizeX); void fnFillArray (int aiX[], int iSizeX); void fnPrintArray (const int aiX[], int iSizeX); int fnSumArray (const int aiX[], int iSizeX); int fnIndexLargestElement (const int aiX[], int iSizeX); void fnCopyArray (const int aiX[], int aiY[], int iLength); int main() { int aiListA [iArraySize] = {0}; int aiListB [iArraySize]; fnPrintArray (aiListA, iArraySize); fnInitializeArray (aiListB, iArraySize); fnPrintArray (aiListB, iArraySize); fnFillArray (aiListA, iArraySize); fnPrintArray (aiListA, iArraySize); fnSumArray (aiListA, iArraySize); fnCopyArray (aiListA, aiListB, iArraySize); fnPrintArray (aiListB, iArraySize); return 0; } void fnInitializeArray (int aiX[ ], int iSizeX) { int iCounter; for (iCounter = 0; iCounter < iSizeX; iCounter++) aiX[iCounter] = 0; }

PGT 106 C Programming Using the const Qualifier with Pointers const qualifier Variable cannot be changed Use const if function does not need to change a variable Attempting to change a const variable produces an error const pointers Point to a constant memory location Must be initialized when defined int *const piMyPtr = &iX; Type int *const – constant pointer to an int const int *piMyPtr = &iX; Regular pointer to a const int const int *const piPtr = &iX; const pointer to a const int iX can be changed, but not *piPtr

PGT 106 C Programming 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

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

PGT 106 C Programming Pointer Expressions and Pointer Arithmetic Subtracting pointers Returns number of elements from one to the other. If piVPtr2 = &aiV[ 2 ]; piVPtr = &aiV[ 0 ]; piVPtr2 - piVPtr would produce 2 Pointer comparison ( ) See which pointer points to the higher numbered array element Also, see if a pointer points to 0

PGT 106 C Programming Pointer Expressions and Pointer Arithmetic 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

UniMAP SemII-09/10PGT 106 C Programming Example of Pointer Operation #include int main() {int *piVPtr; int *piVPtr2; int aiV[5] = {10,20,30,40,50}; int iTemp; int *piP, *piQ; piVPtr= aiV; printf("Address of piVPtr : %d Contents of piVPtr : %d\n", &piVPtr, piVPtr); printf("Address of aiV[0] : %d\n", &aiV); piVPtr +=2; printf("Address of piVPtr + 2: %d\n", piVPtr); piVPtr +=2; printf("Address of piVPtr + 4: %d\n", piVPtr); piVPtr2=&aiV[2]; piVPtr=&aiV[0]; iTemp=piVPtr2-piVPtr; printf("Contents of iTemp : %d\n", iTemp); piP=piQ; printf("Contents of piP : %d piQ : %d\n", piP, piQ); return 0;} Address of piVPtr : Contents of piVPtr : Address of aiV[0] : Address of piVPtr + 2: Address of piVPtr + 4: Contents of temp : 2 Contents of piP : piQ :

PGT 106 C Programming 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 aiB[5] and a pointer piBPtr To set them equal to one another use: piBPtr = aiB; The array name (aiB) is actually the address of first element of the array aiB [ 5 ] piBPtr = &aiB[0]; Explicitly assigns piBPtr to the address of first element of aiB

PGT 106 C Programming The Relationship between Pointers and Arrays Element aiB[3] Can be accessed by *(piBPtr + 3) Where * is the offset. Called pointer/offset notation Can be accessed by piBPtr[3] Called pointer/subscript notation piBPtr[3] same as aiB[3] Can be accessed by performing pointer arithmetic on the array itself *(aiB + 3)

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

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

PGT 106 C Programming Arrays of Pointers acSuit array has a fixed size, but strings can be of any size acSuit[3] acSuit[2] acSuit[1] acSuit[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’

PGT 106 C Programming Example #include #define N 5 int main() { char *acStudentName[N]; int iIndex; for(iIndex=0;iIndex<5;iIndex++) { printf("Enter student[%d] name : ", iIndex); scanf("%s", acStudentName + iIndex); printf("You just entered :\n%s\n", acStudentName + iIndex); } 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