c-13

17
Lecture 13 Lecture 13 Version 1.0 Version 1.0 Introduction to Pointers Introduction to Pointers

Upload: rushdi-shams

Post on 19-Jan-2015

31 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

  • 1. Lecture 13Lecture 13 Version 1.0Version 1.0 Introduction to PointersIntroduction to Pointers

2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh PreludePrelude The most crucial of all of Cs grammars isThe most crucial of all of Cs grammars is pointerspointers Many other programming languages have theMany other programming languages have the similar concept as pointer in Csimilar concept as pointer in C they merely do not use the best of pointersthey merely do not use the best of pointers C uses pointers as its inevitable partC uses pointers as its inevitable part once you have the clean idea on pointers, youonce you have the clean idea on pointers, you will see that by using pointers, you can solve anywill see that by using pointers, you can solve any problem in Cproblem in C 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of Variable, Value &Concept of Variable, Value & AddressAddress int i = 3;int i = 3; This declaration tells the C compiler to:This declaration tells the C compiler to: (a) Reserve space in memory to hold the integer(a) Reserve space in memory to hold the integer value.value. (b) Associate the name(b) Associate the name ii with this memorywith this memory location.location. (c) Store the value 3 at this location.(c) Store the value 3 at this location. 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of Variable, Value &Concept of Variable, Value & AddressAddress The important point is,The important point is, iis address in memory is as address in memory is a number.number. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of Variable, Value &Concept of Variable, Value & AddressAddress 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of Variable, Value &Concept of Variable, Value & AddressAddress The other pointer operator available in C is The other pointer operator available in C is ** called value at address operatorcalled value at address operator It gives the value stored at a particular addressIt gives the value stored at a particular address The value at address operator is also calledThe value at address operator is also called indirection operatorindirection operator 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of Variable, Value &Concept of Variable, Value & AddressAddress 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer The expressionThe expression &i&i gives the address of thegives the address of the variablevariable ii This address can be collected in a variableThis address can be collected in a variable j = &i ;j = &i ; But remember thatBut remember that jj is not an ordinary variableis not an ordinary variable like any other integer variablelike any other integer variable It is a variable that contains the address of otherIt is a variable that contains the address of other variablevariable 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer As you can see,As you can see, iis value is 3 ands value is 3 and jjs value iss value is iis addresss address 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer But wait, we cant useBut wait, we cant use jj in a program withoutin a program without declaring itdeclaring it sincesince jj is a variable that contains the address ofis a variable that contains the address of ii, it is, it is declared as,declared as, int *j ;int *j ; This declaration tells the compiler thatThis declaration tells the compiler that jj will be usedwill be used to store the address of an integer valueto store the address of an integer value In other wordsIn other words jj points to an integerpoints to an integer 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer int *jint *j would mean, the value at the address containedwould mean, the value at the address contained inin jj is anis an intint 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer Look at the following declarations,Look at the following declarations, 1.1. int *alpha ;int *alpha ; 2.2. char *ch ;char *ch ; 3.3. float *s ;float *s ; The declarationThe declaration float *sfloat *s does not mean thatdoes not mean that ss is going to contain a floating-point valueis going to contain a floating-point value ss is going to contain the address of a floating-is going to contain the address of a floating- point valuepoint value 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer Pointer, we know is a variable that containsPointer, we know is a variable that contains address of another variableaddress of another variable Now this variable itself might be another pointerNow this variable itself might be another pointer Thus, we now have a pointer that containsThus, we now have a pointer that contains another pointers addressanother pointers address 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer i is an ordinaryi is an ordinary intint jj is a pointer to anis a pointer to an intint kk is a pointer to an integer pointeris a pointer to an integer pointer 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer what will be the output?what will be the output?