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?