SlideShare a Scribd company logo
1 of 23
POINTERS
1
J.L. Katugampala SEU/IS/15/ICT/057
M.M.L. Pradeep SEU/IS/15/ICT/004
• First of all, it is a variable, just like other variables you
studied
So it has type, storage etc.
• Difference: it can only store the address (rather than the
value) of a data item
• Type of a pointer variable – pointer to the type of the data
whose address it will store
• Example:
int pointer, float pointer,…
Can be pointer to any user-defined types also
like structure types
What is a pointer,
2
A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location. Like any variable or constant, you must
declare a pointer before you can use it to store any variable address. The
general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and
var-name is the name of the pointer variable. The asterisk * you used to
declare a pointer is the same asterisk that you use for multiplication.
Following are the valid pointer declaration:
int *ip ; /* pointer to an integer */
double *dp ; /* pointer to a double*/
float *fp ; /* pointer to a float */
char *ch ; /* pointer to a character */
3
4
There are few important operations, which we will do with the help of pointers
very frequently.
How to use Pointers,
(a) we define a pointer variable
(b) assign the address of a variable to a pointer.
(c) finally access the value at the address available in the pointer
variable.
This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand.
5
How to use Pointers Cont..
Following example makes use of these operations:
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %xn", &var ); /* address stored in pointer variable */
printf("Address stored in ip variable: %xn", ip ); /* access the value using the pointer */
printf("Value of *ip variable: %dn", *ip );
return 0;
}
6
How to use Pointers Cont..
• When the above code is compiled and executed, it produces result
something as follows:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
7
Pointer Arithmetic
• Pointer arithmetic is one of the most powerful features of C
• If the variable p is a pointer to a particular type, then the
expression p + 1 yields the correct machine address for
storing or accessing the next variable of that type
• Other expressions may be used,
such as: p++, p + i or p += i
8
#include <stdio.h>
int main()
{
double a[2], *p, *q;
p = a; /* points to base of array */
q = p + 1; /* equivalent to q = &a[1]; */
printf ("%dn", q - p);
printf ("%dn", (int)q - (int)p);
return 0;
}
Pointer Arithmetic Example,
Output
1
8
9
• The previous output assumes that a double is stored in 8
bytes
• p points to a double and q points to the next double,
therefore the difference in array elements is 1
• However, the difference in actual memory locations is 8
Pointer Arithmetic Example,
10
Pointer Expressions
11
• The third line tries to assign the address of a double variable to a
pointer to int
• In the fourth line:
The first '*' is the multiplication operator
The second '*' is the dereference operator
• In the fifth line, r is assigned the address of j, and then another
assignment multiplies *r (which now means j) by *p
Pointer Expressions
12
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the
variable pointer can be incremented, unlike the array name which cannot be
incremented because it is a constant pointer. The following program
increments the variable pointer to access each succeeding element of the
array:
13
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr; /* let us have array address in pointer */
ptr = &var;
for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %xn", i, ptr );
printf("Value of var[%d] = %dn", i, *ptr ); /* move to the next location */
ptr++;
}
return 0;
Incrementing a Pointer cont…
14
When the above code is compiled and executed, it produces result
something as follows:
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
Incrementing a Pointer cont…
15
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its value by
the number of bytes of its data type as shown below:
#include <stdio.h>
int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr; /* let us have array address in pointer */
ptr = &var;
for ( i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %xn", i, ptr );
printf("Value of var[%d] = %dn", i, *ptr ); /* move to the previous
location */
ptr--;
}
return 0;
16
Decrementing a Pointer cont…
When the above code is compiled and executed, it produces result
something as follows:
Address of var[3] = bfedbcd8
Value of var[3] = 200
Address of var[2] = bfedbcd4
Value of var[2] = 100
Address of var[1] = bfedbcd0
Value of var[1] = 10
17
Pointer Comparisons
Pointers may be compared by using relational operators, such as ==, <, and
>. If p1 and p2 point to variables that are related to each other, such as
elements of the same array, then p1 and p2 can be meaningfully compared.
The following program modifies the previous example one by incrementing
the variable pointer so long as the address to which it points is either less
than or equal to the address of the last element of the array, which is
&var[MAX - 1]:
18
Pointer Comparisons cont…
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr; /* let us have address of the first element in
pointer */
ptr = var;
i = 0;
while ( ptr <= &var[MAX - 1] )
{
printf("Address of var[%d] = %xn", i, ptr );
printf("Value of var[%d] = %dn", i, *ptr ); /* point to the
previous location */
ptr++;
i++;
}
return 0;
19
Pointer Comparisons cont…
When the above code is compiled and executed, it produces result
something as follows:
Address of var[0] = bfdbcb20
Value of var[0] = 10
Address of var[1] = bfdbcb24
Value of var[1] = 100
Address of var[2] = bfdbcb28
Value of var[2] = 200
20
Pointers to Functions
Pointers to Functions
• Contains address of function
• Similar to how array name is address of first element
• Function name is starting address of code that defines function
Function pointers can be
• Passed to functions
• Stored in arrays
• Assigned to other function pointers
21
Function bubble takes a function pointer
• bubble calls this helper function
• this determines ascending or descending sorting
The argument in bubblesort for the function pointer:
int ( *compare )( int a, int b )
tells bubblesort to expect a pointer to a function that takes two
ints and returns an int
If the parentheses were left out:
int *compare( int a, int b )
Defines a function that receives two integers and returns a pointer to a int
Pointers to Functions Example: bubblesort
REFERENCES,
22
https://www.slideshare.net/jass91/pointers-in-c-
programming
https://www.w3schools.com/c/c_pointers.php
23
THANK YOU….

More Related Content

Similar to pointers.pptx (20)

PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers
PointersPointers
Pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 

Recently uploaded

Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesChandrakantDivate1
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 

Recently uploaded (20)

Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 

pointers.pptx

  • 2. • First of all, it is a variable, just like other variables you studied So it has type, storage etc. • Difference: it can only store the address (rather than the value) of a data item • Type of a pointer variable – pointer to the type of the data whose address it will store • Example: int pointer, float pointer,… Can be pointer to any user-defined types also like structure types What is a pointer, 2
  • 3. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is: type *var-name; Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. Following are the valid pointer declaration: int *ip ; /* pointer to an integer */ double *dp ; /* pointer to a double*/ float *fp ; /* pointer to a float */ char *ch ; /* pointer to a character */ 3
  • 4. 4 There are few important operations, which we will do with the help of pointers very frequently. How to use Pointers, (a) we define a pointer variable (b) assign the address of a variable to a pointer. (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
  • 5. 5 How to use Pointers Cont.. Following example makes use of these operations: #include <stdio.h> int main () { int var = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &var; /* store address of var in pointer variable*/ printf("Address of var variable: %xn", &var ); /* address stored in pointer variable */ printf("Address stored in ip variable: %xn", ip ); /* access the value using the pointer */ printf("Value of *ip variable: %dn", *ip ); return 0; }
  • 6. 6 How to use Pointers Cont.. • When the above code is compiled and executed, it produces result something as follows: Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20
  • 7. 7 Pointer Arithmetic • Pointer arithmetic is one of the most powerful features of C • If the variable p is a pointer to a particular type, then the expression p + 1 yields the correct machine address for storing or accessing the next variable of that type • Other expressions may be used, such as: p++, p + i or p += i
  • 8. 8 #include <stdio.h> int main() { double a[2], *p, *q; p = a; /* points to base of array */ q = p + 1; /* equivalent to q = &a[1]; */ printf ("%dn", q - p); printf ("%dn", (int)q - (int)p); return 0; } Pointer Arithmetic Example, Output 1 8
  • 9. 9 • The previous output assumes that a double is stored in 8 bytes • p points to a double and q points to the next double, therefore the difference in array elements is 1 • However, the difference in actual memory locations is 8 Pointer Arithmetic Example,
  • 11. 11 • The third line tries to assign the address of a double variable to a pointer to int • In the fourth line: The first '*' is the multiplication operator The second '*' is the dereference operator • In the fifth line, r is assigned the address of j, and then another assignment multiplies *r (which now means j) by *p Pointer Expressions
  • 12. 12 Incrementing a Pointer We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer. The following program increments the variable pointer to access each succeeding element of the array:
  • 13. 13 #include <stdio.h> int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have array address in pointer */ ptr = &var; for ( i = 0; i < MAX; i++) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); /* move to the next location */ ptr++; } return 0; Incrementing a Pointer cont…
  • 14. 14 When the above code is compiled and executed, it produces result something as follows: Address of var[0] = bf882b30 Value of var[0] = 10 Address of var[1] = bf882b34 Value of var[1] = 100 Address of var[2] = bf882b38 Value of var[2] = 200 Incrementing a Pointer cont…
  • 15. 15 Decrementing a Pointer The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type as shown below: #include <stdio.h> int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have array address in pointer */ ptr = &var; for ( i = MAX; i > 0; i--) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); /* move to the previous location */ ptr--; } return 0;
  • 16. 16 Decrementing a Pointer cont… When the above code is compiled and executed, it produces result something as follows: Address of var[3] = bfedbcd8 Value of var[3] = 200 Address of var[2] = bfedbcd4 Value of var[2] = 100 Address of var[1] = bfedbcd0 Value of var[1] = 10
  • 17. 17 Pointer Comparisons Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared. The following program modifies the previous example one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the array, which is &var[MAX - 1]:
  • 18. 18 Pointer Comparisons cont… #include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have address of the first element in pointer */ ptr = var; i = 0; while ( ptr <= &var[MAX - 1] ) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); /* point to the previous location */ ptr++; i++; } return 0;
  • 19. 19 Pointer Comparisons cont… When the above code is compiled and executed, it produces result something as follows: Address of var[0] = bfdbcb20 Value of var[0] = 10 Address of var[1] = bfdbcb24 Value of var[1] = 100 Address of var[2] = bfdbcb28 Value of var[2] = 200
  • 20. 20 Pointers to Functions Pointers to Functions • Contains address of function • Similar to how array name is address of first element • Function name is starting address of code that defines function Function pointers can be • Passed to functions • Stored in arrays • Assigned to other function pointers
  • 21. 21 Function bubble takes a function pointer • bubble calls this helper function • this determines ascending or descending sorting The argument in bubblesort for the function pointer: int ( *compare )( int a, int b ) tells bubblesort to expect a pointer to a function that takes two ints and returns an int If the parentheses were left out: int *compare( int a, int b ) Defines a function that receives two integers and returns a pointer to a int Pointers to Functions Example: bubblesort