Sie sind auf Seite 1von 18

POINTER ARITHMETIC

Page 1
Introduction
• A pointer in C is an address, which is a
numeric value.
 Therefore , we can perform arithmetic operations
on a pointer.

• There are four arithmetic operators that can


be used on pointers
 ++
 --
 +
 -

Page 2
Concept

• When we perform any arithmetic function


on a pointer, changes occur as per the
size of their primitive data type.

Page 3
Simple Example of Pointer Arithmetic

int *i;
i++ ;

• Pointer size is 2 bytes


• Pointer variable will get incremented by 2
bytes, as the primitive data type “int” is of
2 bytes.

Page 4
Simple Example of Pointer
Arithmetic
float *i;
i++ ;

• Pointer size is 4 bytes


• Pointer variable will get incremented by 4
bytes, as the primitive data type “float” is
of 4 bytes.

Page 5
POINTER ARITHMETIC OPERATIONS

1. Incrementing pointer
2. Decrementing a pointer
3. Addition of Pointer and a number
4. Subtraction of Pointer and a number
5. Differencing between two Pointers
6. Comparison between two Pointers

Page 6
1. Incrementing pointer

• Incrementing Pointer is generally used in


array because we have contiguous
memory in array and we know that the
contents of next memory location.

• Incrementing Pointer Variable depends


upon the data type of the Pointer
Variable.

Page 7
1. Incrementing pointer

DATA TYPE OLDER ADDRESS NEXT ADDRESS STORED IN


STORED IN POINTER POINTER AFTER
INCREMENTING (ptr++)
int 1000 1002

float 1000 1004

char 1000 1001

Page 8
1. Incrementing pointer
main()
{
int *ptr = (int *) 1000 ;
ptr++; //ptr = ptr + 1 ;
printf(“ new value of ptr : %d”, ptr);
}

OUTPUT:
New value of ptr : 1002

Page 9
2. Decrementing pointer

DATA TYPE OLDER ADDRESS NEXT ADDRESS STORED IN


STORED IN POINTER POINTER AFTER
INCREMENTING(ptr--)
int 1000 998

float 1000 996

char 1000 999

Page 10
2. Decrementing pointer
main()
{
int *ptr = (int *) 1000 ;
ptr-- ; //ptr = ptr - 1 ;
printf(“ new value of ptr : %d”, ptr);
}

OUTPUT:
New value of ptr : 998

Page 11
3. Addition of Pointer and a number
main()
{
int *ptr = (int *)1000;
ptr = ptr + 3;
printf(“ new value of ptr : %d”, ptr);
}
OUTPUT:
New value of ptr : 1006

Page 12
4. Subtraction of Pointer and a number
main()
{
int *ptr = (int *)1000;
ptr = ptr - 3;
printf(“ new value of ptr : %d”, ptr);
}
OUTPUT:
New value of ptr : 994

Page 13
5. Differencing between two Pointers

• Differencing means Subtracting two


Pointers.

• Subtraction gives the “total number of


objects between them”.

• Subtraction indicates “How apart the two


Pointers are?”.

Page 14
5. Differencing between two Pointers

main()
{
float *ptr1 = (float *)1000;
float *ptr2 = (float *)2000;
printf(“ Difference : %d”, ptr2 – ptr1);
}
OUTPUT:
Difference : 250

Page 15
6. Comparison between two Pointers
• Pointer comparison is valid only if the two
pointers are pointing to same array.
• All relational operators can be used for
comparing pointers of same type/different
type.
• All equality and inequality operators can
be used with all pointer types.
• Pointers cannot be multiplied or divided.

Page 16
EXAMPLE
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
ptr1 = (int *)1000;
ptr2 = (int *)2000;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}

Page 17
Page 18

Das könnte Ihnen auch gefallen