• Like
C-13
Upcoming SlideShare
Loading in...5
×

C-13

  • 25 views
Uploaded on

 

More in: Education , Technology , Travel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
25
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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 C’s grammars isThe most crucial of all of C’s 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, ii’s address in memory is a’s 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’ operator‘indirection’ 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, ii’s value is 3 and’s value is 3 and jj’s value is’s value is ii’s address’s address
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh Concept of PointerConcept of Pointer  But wait, we can’t useBut wait, we can’t 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 pointer’s addressanother pointer’s 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?