SlideShare a Scribd company logo
1 of 25
Agenda
 Introduction
 Arithematic on a Pointer
 NULLPointer
 Pointers Comparison
 Function Pointers
 Pointers andArrays
 Arrays of Pointers
Pointer to Pointer
WHAT IS A V
VA
AR
RI
IA
AB
BL
LE
E?
»Avariable is something with a name.
»The value of that variable is not fixed.
»Size of the variable depends on the datatype.
»The program on the right will tell you the size
of the variables in your system.
#include <stdio.h>
int main()
{
printf(“size of a short is %dn”,
sizeof(short));
printf(“size of a int is %dn”,
sizeof(int));
printf(“size of a long is %dn”,
sizeof(long));
}
DECLARING A VARIABLE IN C
 int k ;
 Whatdoes the above statement mean?
Int means the compiler needs to set aside 4 bytes of memory to
hold this particular value k.
 This variable k is going to be stored in the computers RAM.
 k = 2 ;
 Now the value 2 is going to be placed in the memory location for k.
So if we check the place in RAM where the variable is stored, we
will find 2 there.
WHAT IS POINTER ?
» Apointer is a variable which contains the address (in memory)of another
variable.
» Pointers are symbolic representation of addresses.
» We can have a pointer to any variable type.
var
( Normal Variable )
ptr
( Pointer Variable )
647 1001
1001 5001
Operators used in Pointers
&
Address
Dereferencing
*)
of
(Value
(Address of)
The unary or monadic operator & gives the
“address of a variable”
The indirection or dereference operator
* gives the ``contents of an object
pointed to by a pointer''.
POINTER VARIABLE INITIALIZATION
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
POINTER VARIABLE INITIALIZATION
int i ;
int * p ;
p = & i;
X1000
Int i=3;
Value of ‘i’
X1000
p
Address of ‘i’
‘&i’
i
3
(Value of i)
Address of i
The value ‘3’ is saved in the memory location ‘x1000’
‘*p’
POINTER EXAMPLES
int x=1, y=2;
int *ptr;
ptr = &x; x y ptr
1 2 100
200
100 1000
y ptr
1 1 100
200
100 1000
y = * ptr; x
NULL POINTER
 NULL Pointer is a pointer which is pointing to nothing.
 NULL pointer points the base address of segment.
 In case, if you don’t have address to be assigned to pointer then you can
simply use NULL
Pointer which is initialized with NULL value is considered as NULL
pointer.
 NULL is macro constant defined in following header files –
<stdio.h>
<alloc.h>
<mem.h>
<stddef.h>
Defining NULL
Value
# define NULL 0
float *ptr = (float *)0; char
*ptr = (char *)0; double
*ptr = (double *)0; char
*ptr = '0';
int *ptr = NULL;
#include <stdio.h>
int main()
{
int *ptr = NULL;
printf("The value of
ptr is %u",ptr);
return 0;
}
OUTPUT
0
POINTER ARITHEMATIC
In C pointer holds address of a value, so there can be arithmetic operations
on the pointer variable. Following arithmetic operations are possible on
pointer in C language:
 Increment
 Decrement
 Addition
 Subtraction
 Comparison
Pointer increment
new address= current address + i *
sizeof(data type)
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p+1;
printf("After increment: Address of p vari
able is %u n",p);
}
Address of p variable is 3214864300 After increment:
Address of p variable is 3214864304
e
Pointer Decrement
Newaddress=current address -i * sizeof(datatype)
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p-1;
printf("After increment: Address of p vari
able is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864296
Pointer Addition
Newaddress=currentaddress + (number * sizeof(datatype))
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p+3;
printf("After adding
3: Address of p variable is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864312
Pointer Substraction
newaddress=current address -(number * sizeof(datatype))
#include <stdio.h>
void main()
{
int number=50;
int *p; //pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p)
;
p=p-3; //substracting 3 from pointer variable
printf("After substracting
3: Address of p variable is %u n",p);
}
Address of p variable is 3214864300 After decrement:
Address of p variable is 3214864288
 The comparison is valid only between pointers that point to the same array.
 The following relational operators work for pointers operation:
==, !=, >, <, >= and <=
 Alower array element that is those having a smaller subscript, always have a lower address than
the higher array elements.
 Thus if pPointer1 and pPointer2 pointing to the elements of the same array, the following
comparison is TRUE,
pPointer1 < pPointer2
 If pPointer1 points to an earlier member of the array than pPointer2 does.
 Many arithmetic operations that can be performed with regular variables, such as multiplication
and division, do not work with pointers and will generate errors in C.
POINTERS COMPARISON
Operation Description
1. Assignment (=)
You can assign a value to a pointer. The value should be an address with
the address-of-operator(&) or from a pointer constant (array name)
2. Indirection (*)
The indirection operator(*) gives the value stored in the pointed to
location.
3. Address of (&)
You can use the address-of operatorto find the address of a pointer, so
you can use pointers to pointers.
4. Incrementing You can add an integer to a pointer to point to a different memorylocation.
5. Differencing
You can subtractan integer from a pointer to point to a different memory
location.
6. Comparison Valid only with two pointers that point to the same array.
The following table summarizes pointer operations.
FUNCTION POINTER
 In C, like normal data pointers (int *, char *, etc), we can have pointers to functions.
Unlike normal pointers, a function pointer points to code, not data. Typically a function
pointer stores the start of executable code.
 Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
#include<stdio.h>
void display();
int main()
{
void *(*ptr)();
ptr = &display;
(*ptr)(); return(0);
}
void display()
{
printf("Hello World");
}
OUTPUT
Hello World
POINTERS AND ARRAYS
int a[10] , x ;
int *pa ;
pa = &a [0] /* pa pointer to address of a[0] */
x = * pa /* x = contains of pa (a[0] in this case) */
 pa = a; instead of pa = &a[0]
 a[i] can be written as *(pa + i)
a
ARRAYS OF POINTERS
 Pointers may be arrayed like any other data type.
 For example, a pointer array iArrayPtr of sized 20 is declared as,
int *iArrayPtr[20]; // assign the address of variable
 Toassigntheaddressof aninteger variable called iIntVar to thefirst
element of the array, we could write somethinglike this.
iArrayPtr [0] = &iIntVar; // iIntVar to the first iArrayPtr element
POINTER TO POINTER
A pointer can also be made to point to a
pointer variable.
Example:
int V = 100;
int * P = &V; / * P points to int V */
int * * Q = &P; / * Q points to int pointer P * /
printf(“%d %d %dn”,V,*P,**Q); / * prints 101 3 times */
As per the figure , pr2 is a pointer for num(as pr2 is having address of variable
num),similarly pr1 is a pointer for another pointer pr1 (as pr1 is having the address
of pointer pr2).A pointer ehich points to another pointer is known as double pointer.
Variable numhasAddress : X7730
Address of Pointer pr1 :X6611
Address of Pointer pr2 : X6698
X6698 X7730 123
pr1 pr2 num
X7730
X6698
X6611
#include <stdio.h>
int main( )
{
int num = 123;
int *pr2;
int ** pr1;
// Pointer for num
//Double pointer for pr2
pr2 = &num; //Address of variable num and * storing it in pointer pr2
pr1 = &pr2; //Storing the address of pointer pr2 into pointer pr1
/* POSSIBLE WAYS TO FIND THE VALUES OF VARIABLE NUM*/
printf(“n Value of num is: %d”,num);
printf(“n Value of num using pr2 is: %d”, *pr2);
printf(“n Value of num using pr1 is: %d”, **pr1);
/* POSSIBLE WAYS TO FIND ADDRESS OF NUM */
printf(“n Address of num is: %U”,&num);
printf(“n Address of num using pr2 is: %u”, pr2);
printf(“n Address of num using pr1 is: %u”, *pr1);
/* FIND VALUE OF POINTER */
printf(“n Value of pointer pr2 is : %u”,pr2);
printf(“n Value of pointer pr2 using pr1 is : %u”,*pr1);
/* WAYS TO FIND ADDRESS OF POINTER */
Printf(“ n Address of pointer pr2 is :%u”,&pr2);
Printf(“ n Address of pointer pr1 is :%u”,&pr1);
}
THANKS

More Related Content

Similar to Unit-I Pointer Data structure.pptx

pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
AmitRout25
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 

Similar to Unit-I Pointer Data structure.pptx (20)

Pointers
PointersPointers
Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
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 in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Pointers
PointersPointers
Pointers
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
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++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
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...
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 

More from ajajkhan16

More from ajajkhan16 (15)

Unit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfUnit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdf
 
data stream processing.and its applications pdf
data stream processing.and its applications pdfdata stream processing.and its applications pdf
data stream processing.and its applications pdf
 
data streammining and its applications.ppt
data streammining and its applications.pptdata streammining and its applications.ppt
data streammining and its applications.ppt
 
NOSQL in big data is the not only structure langua.pdf
NOSQL in big data is the not only structure langua.pdfNOSQL in big data is the not only structure langua.pdf
NOSQL in big data is the not only structure langua.pdf
 
Big-Data 5V of big data engineering.pptx
Big-Data 5V of big data engineering.pptxBig-Data 5V of big data engineering.pptx
Big-Data 5V of big data engineering.pptx
 
binarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdfbinarysearchtreeindatastructures-200604055006 (1).pdf
binarysearchtreeindatastructures-200604055006 (1).pdf
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
sparkbigdataanlyticspoweerpointpptt.pptx
sparkbigdataanlyticspoweerpointpptt.pptxsparkbigdataanlyticspoweerpointpptt.pptx
sparkbigdataanlyticspoweerpointpptt.pptx
 
20-security.ppt
20-security.ppt20-security.ppt
20-security.ppt
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
key1.pdf
key1.pdfkey1.pdf
key1.pdf
 
08 MK-PPT Advanced Topic 2.ppt
08 MK-PPT Advanced Topic 2.ppt08 MK-PPT Advanced Topic 2.ppt
08 MK-PPT Advanced Topic 2.ppt
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
Day2.pptx
Day2.pptxDay2.pptx
Day2.pptx
 
Unit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptxUnit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptx
 

Recently uploaded

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
hublikarsn
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 

Recently uploaded (20)

Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
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
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
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
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
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
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
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
 

Unit-I Pointer Data structure.pptx

  • 1. Agenda  Introduction  Arithematic on a Pointer  NULLPointer  Pointers Comparison  Function Pointers  Pointers andArrays  Arrays of Pointers Pointer to Pointer
  • 2. WHAT IS A V VA AR RI IA AB BL LE E? »Avariable is something with a name. »The value of that variable is not fixed. »Size of the variable depends on the datatype. »The program on the right will tell you the size of the variables in your system. #include <stdio.h> int main() { printf(“size of a short is %dn”, sizeof(short)); printf(“size of a int is %dn”, sizeof(int)); printf(“size of a long is %dn”, sizeof(long)); }
  • 3. DECLARING A VARIABLE IN C  int k ;  Whatdoes the above statement mean? Int means the compiler needs to set aside 4 bytes of memory to hold this particular value k.  This variable k is going to be stored in the computers RAM.  k = 2 ;  Now the value 2 is going to be placed in the memory location for k. So if we check the place in RAM where the variable is stored, we will find 2 there.
  • 4. WHAT IS POINTER ? » Apointer is a variable which contains the address (in memory)of another variable. » Pointers are symbolic representation of addresses. » We can have a pointer to any variable type. var ( Normal Variable ) ptr ( Pointer Variable ) 647 1001 1001 5001
  • 5. Operators used in Pointers & Address Dereferencing *) of (Value (Address of) The unary or monadic operator & gives the “address of a variable” The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.
  • 6. POINTER VARIABLE INITIALIZATION Basic syntax: Type *Name Examples: int *P; /* P is var that can point to an int var */ float *Q; /* Q is a float pointer */ char *R; /* R is a char pointer */ Complex example: int *AP[5]; /* AP is an array of 5 pointers to ints */
  • 7. POINTER VARIABLE INITIALIZATION int i ; int * p ; p = & i; X1000 Int i=3; Value of ‘i’ X1000 p Address of ‘i’ ‘&i’ i 3 (Value of i) Address of i The value ‘3’ is saved in the memory location ‘x1000’ ‘*p’
  • 8. POINTER EXAMPLES int x=1, y=2; int *ptr; ptr = &x; x y ptr 1 2 100 200 100 1000 y ptr 1 1 100 200 100 1000 y = * ptr; x
  • 9. NULL POINTER  NULL Pointer is a pointer which is pointing to nothing.  NULL pointer points the base address of segment.  In case, if you don’t have address to be assigned to pointer then you can simply use NULL Pointer which is initialized with NULL value is considered as NULL pointer.  NULL is macro constant defined in following header files – <stdio.h> <alloc.h> <mem.h> <stddef.h>
  • 10. Defining NULL Value # define NULL 0 float *ptr = (float *)0; char *ptr = (char *)0; double *ptr = (double *)0; char *ptr = '0'; int *ptr = NULL; #include <stdio.h> int main() { int *ptr = NULL; printf("The value of ptr is %u",ptr); return 0; } OUTPUT 0
  • 11. POINTER ARITHEMATIC In C pointer holds address of a value, so there can be arithmetic operations on the pointer variable. Following arithmetic operations are possible on pointer in C language:  Increment  Decrement  Addition  Subtraction  Comparison
  • 12. Pointer increment new address= current address + i * sizeof(data type) #include <stdio.h> void main() { int number=50; int *p; //pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p) ; p=p+1; printf("After increment: Address of p vari able is %u n",p); } Address of p variable is 3214864300 After increment: Address of p variable is 3214864304 e
  • 13. Pointer Decrement Newaddress=current address -i * sizeof(datatype) #include <stdio.h> void main() { int number=50; int *p; //pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p) ; p=p-1; printf("After increment: Address of p vari able is %u n",p); } Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296
  • 14. Pointer Addition Newaddress=currentaddress + (number * sizeof(datatype)) #include <stdio.h> void main() { int number=50; int *p; //pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p) ; p=p+3; printf("After adding 3: Address of p variable is %u n",p); } Address of p variable is 3214864300 After decrement: Address of p variable is 3214864312
  • 15. Pointer Substraction newaddress=current address -(number * sizeof(datatype)) #include <stdio.h> void main() { int number=50; int *p; //pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p) ; p=p-3; //substracting 3 from pointer variable printf("After substracting 3: Address of p variable is %u n",p); } Address of p variable is 3214864300 After decrement: Address of p variable is 3214864288
  • 16.  The comparison is valid only between pointers that point to the same array.  The following relational operators work for pointers operation: ==, !=, >, <, >= and <=  Alower array element that is those having a smaller subscript, always have a lower address than the higher array elements.  Thus if pPointer1 and pPointer2 pointing to the elements of the same array, the following comparison is TRUE, pPointer1 < pPointer2  If pPointer1 points to an earlier member of the array than pPointer2 does.  Many arithmetic operations that can be performed with regular variables, such as multiplication and division, do not work with pointers and will generate errors in C. POINTERS COMPARISON
  • 17. Operation Description 1. Assignment (=) You can assign a value to a pointer. The value should be an address with the address-of-operator(&) or from a pointer constant (array name) 2. Indirection (*) The indirection operator(*) gives the value stored in the pointed to location. 3. Address of (&) You can use the address-of operatorto find the address of a pointer, so you can use pointers to pointers. 4. Incrementing You can add an integer to a pointer to point to a different memorylocation. 5. Differencing You can subtractan integer from a pointer to point to a different memory location. 6. Comparison Valid only with two pointers that point to the same array. The following table summarizes pointer operations.
  • 18. FUNCTION POINTER  In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.  Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
  • 19. #include<stdio.h> void display(); int main() { void *(*ptr)(); ptr = &display; (*ptr)(); return(0); } void display() { printf("Hello World"); } OUTPUT Hello World
  • 20. POINTERS AND ARRAYS int a[10] , x ; int *pa ; pa = &a [0] /* pa pointer to address of a[0] */ x = * pa /* x = contains of pa (a[0] in this case) */  pa = a; instead of pa = &a[0]  a[i] can be written as *(pa + i) a
  • 21. ARRAYS OF POINTERS  Pointers may be arrayed like any other data type.  For example, a pointer array iArrayPtr of sized 20 is declared as, int *iArrayPtr[20]; // assign the address of variable  Toassigntheaddressof aninteger variable called iIntVar to thefirst element of the array, we could write somethinglike this. iArrayPtr [0] = &iIntVar; // iIntVar to the first iArrayPtr element
  • 22. POINTER TO POINTER A pointer can also be made to point to a pointer variable. Example: int V = 100; int * P = &V; / * P points to int V */ int * * Q = &P; / * Q points to int pointer P * / printf(“%d %d %dn”,V,*P,**Q); / * prints 101 3 times */
  • 23. As per the figure , pr2 is a pointer for num(as pr2 is having address of variable num),similarly pr1 is a pointer for another pointer pr1 (as pr1 is having the address of pointer pr2).A pointer ehich points to another pointer is known as double pointer. Variable numhasAddress : X7730 Address of Pointer pr1 :X6611 Address of Pointer pr2 : X6698 X6698 X7730 123 pr1 pr2 num X7730 X6698 X6611
  • 24. #include <stdio.h> int main( ) { int num = 123; int *pr2; int ** pr1; // Pointer for num //Double pointer for pr2 pr2 = &num; //Address of variable num and * storing it in pointer pr2 pr1 = &pr2; //Storing the address of pointer pr2 into pointer pr1 /* POSSIBLE WAYS TO FIND THE VALUES OF VARIABLE NUM*/ printf(“n Value of num is: %d”,num); printf(“n Value of num using pr2 is: %d”, *pr2); printf(“n Value of num using pr1 is: %d”, **pr1); /* POSSIBLE WAYS TO FIND ADDRESS OF NUM */ printf(“n Address of num is: %U”,&num); printf(“n Address of num using pr2 is: %u”, pr2); printf(“n Address of num using pr1 is: %u”, *pr1); /* FIND VALUE OF POINTER */ printf(“n Value of pointer pr2 is : %u”,pr2); printf(“n Value of pointer pr2 using pr1 is : %u”,*pr1); /* WAYS TO FIND ADDRESS OF POINTER */ Printf(“ n Address of pointer pr2 is :%u”,&pr2); Printf(“ n Address of pointer pr1 is :%u”,&pr1); }