KEMBAR78
Pointers Arithmetic Operations | PDF | Pointer (Computer Programming) | Integer (Computer Science)
0% found this document useful (0 votes)
47 views30 pages

Pointers Arithmetic Operations

Pointer arithmetic allows incrementing, decrementing, adding, and subtracting integers from pointers. However, some operations are invalid, such as adding two addresses together. Pointer comparisons are only valid between pointers pointing to the same array. Invalid pointer operations include addition, multiplication, division, and bitwise operations between two addresses.

Uploaded by

Nishok Nishok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views30 pages

Pointers Arithmetic Operations

Pointer arithmetic allows incrementing, decrementing, adding, and subtracting integers from pointers. However, some operations are invalid, such as adding two addresses together. Pointer comparisons are only valid between pointers pointing to the same array. Invalid pointer operations include addition, multiplication, division, and bitwise operations between two addresses.

Uploaded by

Nishok Nishok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

POINTERS

Pointer operations-
increment,decrement,addition,subtraction,differ
ence,compare,Invalid pointer operations.
PREPARED BY
Mrs.M.POONKODI
AP/CSE
C pointer arithmetic operations

• Pointer Arithmetic
• Increment /decrement operations
• Addition/Subtraction operations
• Comparison operation

• DT IA OA OR
• int 4000 ++ 4002
• Int4000 - - 3982
• Char 4000 ++ 4001
• Char 4000 - - 3991
• Float 4000 ++ 4004
• Float 4000 - - 3996

• General Address +number


Address of a variable after performing
arithmetic operations
We can see address of an variable after performing arithmetic
operations.
• Expression Result
• Address + Number Address
• Address – Number Address
• Address – Address Number
• Address + Address Illegal
Above table clearly shows that we can add or subtract address
and integer number to get valid address. We can even
subtract two addresses but we cannot add two addresses.
C pointer arithmetic operations-
Incrementing Pointer

• Incrementing Pointer is generally used in array


because we have contiguous memory in array
and we know the contents of next memory
location.
• Incrementing Pointer Variable Depends Upon
data type of the Pointer variable
• Formula : ( After incrementing )
new value = current address + i * size_of(data type)
C pointer arithmetic operations-
Pictorial Representation :
C pointer arithmetic operations-
Increment Integer Pointer

Incremeting Pointer
Incrementing a pointer to an integer data will cause its value to be incremented by 2 .
Live Example 1 : Increment Integer Pointer
#include<stdio.h>
int main()
{
int *ptr=(int *)1000;
ptr=ptr+1;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
New Value of ptr : 1002
Example2- Increment Double Pointer

• #include<stdio.h>
• int main()
• {
• double *ptr=(double *)1000;
• ptr=ptr+1;
• printf("New Value of ptr : %u",ptr);
• return 0; }
Output :
• New Value of ptr : 1004
Example 3 : Array of Pointer 

#include<stdio.h>
int main()
{
float var[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];
ptr=&var;
printf("Value inside ptr : %u",ptr);
ptr=ptr+1;
printf("Value inside ptr : %u",ptr);
return 0;
}
Output :
Value inside ptr : 1000
Value inside ptr : 1020
Diagram
Array of pointers
Array of Pointer
• Explanation :
• Address of ptr[0] = 1000
• We are storing Address of float array to ptr[0].
• Address of ptr[1] = Address of ptr[0] + (Size of Data Type)*(Size of Array)
= 1000 + (4 bytes) * (5)
= 1020
Address of Var[0]…Var[4] :
• Address of var[0] = 1000
• Address of var[1] = 1004
• Address of var[2] = 1008
• Address of var[3] = 1012
• Address of var[4] = 1016
C pointer arithmetic operations-
C decrementing pointer

Formula : ( After decrementing )


new_address = (current address) - i * size_of(data type)

Pointer Program : Difference between two integer Pointers


#include<stdio.h>
int main()
{
float *ptr1=(float *)1000;
float *ptr2=(float *)2000;
printf("\nDifference : %d",ptr2-ptr1);
return 0;
}
Output :
Difference : 250
Explanation :

• Ptr1 and Ptr2 are two pointers which holds


memory address of Float Variable.
• Ptr2-Ptr1 will gives us number of floating point
numbers that can be stored.
• ptr2 - ptr1 = (2000 - 1000) / sizeof(float)
= 1000 / 4 = 250
Example2
#include<stdio.h>
struct var
{
char cvar;
int ivar;
float fvar;
};
int main()
{
struct var *ptr1,*ptr2;
ptr1 = (struct var *)1000;
ptr2 = (struct var *)2000;
printf("Difference= %d",ptr2-ptr1);
return 0;
}
Output :
•Difference = 142
Explanation :

• ptr2-ptr1 = (2000 - 1000)/Sizeof(struct var)


= 1000 / (1+2+4) = 1000 / 7 = 142
C pointer arithmetic operations-
Decrement diagram in integer
C pointer arithmetic operations-ADDITION
Adding integer value with Pointer

Adding integer value with Pointer


In C Programming we can add any integer number to Pointer variable. It is
perfectly legal in c programming to add integer to pointer variable.
In order to compute the final value we need to use following formulae :

Formula
final value = (address) + (number * size of data type)

Consider the following example –


int *ptr , n;
ptr = &n ;
ptr = ptr + 3;
Example 1 : Increment Integer Pointer
#include<stdio.h>
int main()
{
int *ptr=(int *)1000;
ptr=ptr+3;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
New Value of ptr : 1006
Explanation of Program :
In the above program –
int *ptr=(int *)1000;
this line will store 1000 in the pointer variable considering 1000 is memory
location for any of the integer variable.
Formula
Formula :
ptr = ptr + 3 * (sizeof(integer))
= 1000 + 3 * (2)
= 1000 + 6
= 1006
Similarly if we have written above statement like this –
float *ptr=(float *)1000;
then result may be
ptr = ptr + 3 * (sizeof(float))
= 1000 + 3 * (4)
= 1000 + 12
= 1012
C subtracting integer value from pointer -SUBTRACTION

• Suppose we have subtracted “n” from pointer of any data type having
initial addess as “init_address” then after subtraction we can write –
Formula
ptr = initial_address - n * (sizeof(data_type))

Subtracting integer value with Pointer


int *ptr , n;
ptr = &n ;
ptr = ptr - 3;
C subtracting integer value from pointer

#include<stdio.h>
int main()
{
int *ptr=(int *)1000;
ptr=ptr-3;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
New Value of ptr : 994
Formula :
ptr = ptr - 3 * (sizeof(integer)) = 1000 - 3 * (2) = 1000 - 6 = 994
Differencing Pointer in C Programming Language :

 Differencing Means Subtracting two Pointers.


 Subtraction gives the Total number of objects between them .
 Subtraction indicates “How apart the two Pointers are ?”
C Program to Compute Difference Between Pointers :
#include<stdio.h>
int main()
{
int num , *ptr1 ,*ptr2 ;
ptr1 = &num ;
ptr2 = ptr1 + 2 ;
printf("%d",ptr2 - ptr1);
return(0);
}
Output :
2
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
• All Equality and Inequality Operators can be used with all Pointer types
Pointers cannot be Divided or Multiplied
Point 1 : Pointer Comparison
#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);
}
Comparison between two Pointers :
Pointer Comparison of Different Data Types :
#include<stdio.h>
int main()
{
int *ptr1;
float *ptr2;
ptr1 = (int *)1000;
ptr2 = (float *)2000;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0); }
Explanation :
Two Pointers of different data types can be compared .
In the above program we have compared two pointers of different data types.
It is perfectly legal in C Programming.
C pointer Invalid operations
One can perform different arithmetic operations on
Pointer such as increment,decrement but still we have
some more arithmetic operations that cannot be
performed on pointer –
• Addition of two addresses.
• Multiplying two addresses.
• Division of two addresses.
• Modulo operation on pointer.
• Cannot perform bitwise AND,OR,XOR operations on
pointer.
• Cannot perform NOT operation or negation operation.
INVALID POINTER OPERATIONS - EXAMPLES

1. Addition of Two Pointers : 2. Multiplication  of  Pointer


#include<stdio.h> and number :
int main() #include<stdio.h>
{ int main()
int var = 10; {
int *ptr1 = &i; int var = 10;
int *ptr2 = (int *)2000; int *ptr1 = &i;
printf("%d",ptr1+ptr2); int *ptr2 = (int *)2000;
return 0; printf("%d",ptr1*var);
} return 0;
Output : Compile Error }
Output : Compile Error
INVALID POINTER OPERATIONS - EXAMPLES

Multiplication of Two Pointers :


#include<stdio.h>
int main() Output :
{ Compile Error
int var = 10; Similarly we cannot perform
int *ptr1 = &i; following operations –
int *ptr2 = (int *)2000; 4. Modulo Operation
printf("%d",ptr1*ptr2); int *ptr1 = (int *)1000;
return 0; int *ptr2 = (int *)2000;
} int var = ptr2 % ptr1;
INVALID POINTER OPERATIONS - EXAMPLES

5. Division of pointer 6.Cannot perform bitwise OR


#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int *ptr1,*ptr2; int i=5,j=10;
ptr1 = (int *)1000; int *p=&i;
ptr2 = ptr1/4; int *q=&j;
return(0); printf("%d",p|q);
} return 0;
Output }
Error
INVALID POINTER OPERATIONS - EXAMPLES
7.Negation Operation on
Pointer Summary at a glance :
#include<stdio.h> Address + Address = Illegal
int main() Address * Address = Illegal
{ Address / Address = Illegal
int i=5; Address % Address = Illegal
int *ptr=&i; Address & Address = Illegal
printf("%d",~ptr); Address | Address = Illegal
return 0; Address ^ Address = Illegal
} ~Address = Illegal
Double Pointer
Pointer to Pointer in C Programming
Declaration : Double Pointer
int  **ptr2ptr;
Consider the Following Example :
int num = 45 , *ptr , **ptr2ptr ;
ptr = &num;
ptr2ptr = &ptr;

What is Pointer to Pointer ?


Double (**)  is used to denote the double Pointer
Pointer Stores the address of the Variable
Double Pointer Stores the address of the Pointer Variable
Double Pointer

• Pointer to Pointer in C Programming


Declaration : Double Pointer
• int  **ptr2ptr;
Consider the Following Example :
int num = 45 , *ptr , **ptr2ptr ;
ptr = &num;
ptr2ptr = &ptr;
What is Pointer to Pointer ?
• Double (**)  is used to denote the double Pointer
• Pointer Stores the address of the Variable
• Double Pointer Stores the address of the Pointer Variable

You might also like