0% found this document useful (0 votes)
13 views10 pages

Pointers Part 2

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)
13 views10 pages

Pointers Part 2

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/ 10

CSE101-Lec 20

Pointer arithmetic and expressions


Pointer and One dimensional array(or Pointer to 1D array)
Pointer arithmetic
• A limited set of arithmetic operations can be performed on pointers. A
pointer may be:
incremented ( ++ ), e.g. ptr++, ++ptr
decremented (-- ), e.g. ptr--, --ptr
an integer may be added to a pointer ( + or += ), e.g. ptr+2, ptr=ptr+2
an integer may be subtracted from a pointer ( – or -= ), e.g. ptr-2, ptr=ptr-2
We can subtract two pointers, if they are pointing towards same array
We can compare two pointers, if they are pointing towards same array
• Following set of operations are not applicable on pointers
• We cannot add two pointers(addresses)
• We cannot multiply, divide and modulo two pointers(addresses)
• We cannot multiply, divide, modulo any constant from pointer(address)
Pointer arithmetic-Example
#include<stdio.h> //Comparing two pointers
int main() while(p1<=p2)
{ {
int arr[]={1,2,3,4,5,6,7,8,9}; printf("\n%d",*p1);//Comparison of
int *p1,*p2; two pointers (Pointers pointing to the same array)
p1=arr; p1++;
p1++;// p1 will point towards next memory }
location
//Following are the invalid arithmetic
printf("\n%d",*p1);//2 will be displayed operations(Not allowed on pointers)
p1--;//p1 will point towards previous memory //printf("\n%d",p1+p2);//Invalid arithmetic
location
//printf("\n%d",p1/p2);//Invalid arithmetic
printf("\n%d",*p1);// 1 will be displayed
//printf("\n%d",p1*p2);//Invalid arithmetic
p1=p1+2;// Adding a constant to pointer(p1
will point towards 3rd element) //printf("\n%d",p1%p2);//Invalid arithmetic
printf("\n%d",*p1);// 3 will be displayed //printf("\n%d",p1*2);//Invalid arithmetic
p1=p1-2;//Subtracting a constant from a //printf("\n%d",p1/2);//Invalid arithmetic
pointer(P1 will point towards first element) //printf("\n%d",p1%2);//Invalid arithmetic
printf("\n%d",*p1);// 1 will be displayed return 0;
p2=&arr[4]; }
printf("\n%d",p2-p1);//Subtracting two
pointers(Returns 4(no. of elements b/w+1)(Pointers
pointing to the same array)
Pointer expressions
• We can perform rich set of operations like: arithmetic, relational,
assignment, conditional, unary, bitwise on pointer variables
• Examples:
*ptr1 + *ptr2
*ptr1 * *ptr2
*ptr1 + *ptr2 - *ptr3
*ptr1 > *ptr2
*ptr1 < *ptr2
*a=10
*b+=20
*z=3.5
*s=4.56743
c = (*ptr1 > *ptr2) ? *ptr1 : *ptr2;
(*ptr1)++
(*ptr1)--
*ptr1 & *ptr2
*ptr1 | *ptr2
*ptr1 ^ *ptr2
All these are the valid pointer expressions, and here we are working on
values(not on addresses)
Pointer to an array(1D)

• A pointer can point towards an array using following notation:


Consider:
int a[]={1,2,3,4,5};
int *p=a; // pointer p starts pointing towards first element of array
Or
int *p=&a[0];
Now we can access elements of given array via pointer, such as:
int i;
for(i=0;i<5;i++)
{
printf(“\n%d”,*(p+i));
}
The Relationship Between Pointers and Arrays

• Arrays and pointers closely related


• Array name is like a constant pointer
• Pointers can do array subscripting operations
• Define an array b[5] and a pointer bPtr
• To set bPtr to point to b[5]:
bPtr = b;
• The array name (b) is actually the address of first element of the
array b[5] which is equivalent to
bPtr = &b[0]
• Explicitly assigns bPtr to address of first element of b
The Relationship Between Pointers and Arrays

• Element b[3]
• Can be accessed by *(bPtr + 3)
• Where 3 is the offset. Called pointer/offset notation
• Can be accessed by bptr[3]
• Called pointer/subscript notation
• bPtr[3] same as b[3]
• Can be accessed by performing pointer arithmetic on the array
itself
*(b + 3)
• Array name itself is an address or pointer. It points to the first
element(0th element) of array.
• The arrays are accessed by pointers in same way as we access arrays
using array name.
• Consider an array b[5] and a pointer bPtr:
• bPtr[3] is same as b[3]
Example-Different notations with pointer to an array
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5};
int *p=a;
// Different notations with pointer to an array for displaying second element
// Same terminology can be used to display any element
// All will display 2 on screen
printf("\n%d",*(p+1));
printf("\n%d",*(a+1));
printf("\n%d",p[1]);
printf("\n%d",1[p]);
printf("\n%d",1[a]);
return 0;
}
Pointer to an array with pointer arithmetic
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5};
int i;
int *p;
p=arr;
printf("\n First value is:%d",*p);
p=p+1;
printf("\n Second value is:%d",*p);
*p=45;
p=p+2;
*p=-2;
printf("\n Modified array is:");
for(i=0;i<5;i++)
{
printf("\n%d",arr[i]);//We can also write i[arr]
}
p=arr;
*(p+1)=0;
*(p-1)=1;
printf("\n Modified array is:");
for(i=0;i<5;i++)
{
printf("\n%d",*(p+i));//We can also write *(i+arr)
}
return 0;
}

You might also like