0% found this document useful (0 votes)
3 views39 pages

Pointers

The document covers pointer arithmetic and expressions in C, detailing operations that can be performed on pointers, such as incrementing, decrementing, and pointer comparisons. It also explains how to use pointers with one-dimensional arrays and provides examples of pointer expressions and their usage in functions. Additionally, it contrasts pointers with arrays and includes several code examples demonstrating these concepts.

Uploaded by

Kartik Sharma
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)
3 views39 pages

Pointers

The document covers pointer arithmetic and expressions in C, detailing operations that can be performed on pointers, such as incrementing, decrementing, and pointer comparisons. It also explains how to use pointers with one-dimensional arrays and provides examples of pointer expressions and their usage in functions. Additionally, it contrasts pointers with arrays and includes several code examples demonstrating these concepts.

Uploaded by

Kartik Sharma
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/ 39

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>
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int *p1,*p2;
p1=arr;
p1++;// p1 will point towards next memory location
printf("\n%d",*p1);//2 will be displayed
p1--;//p1 will point towards previous memory location
printf("\n%d",*p1);// 1 will be displayed
p1=p1+2;// Adding a constant to pointer(p1 will point towards 3rd element)
printf("\n%d",*p1);// 3 will be displayed
p1=p1-2;//Subtracting a constant from a pointer(P1 will point towards first element)
printf("\n%d",*p1);// 1 will be displayed
p2=&arr[4];
printf("\n%d",p2-p1);//Subtracting two pointers(Returns 4)(Pointers pointing to the same
array)
Pointer arithmetic-Example

//Comparing two pointers


while(p1<=p2)
{
printf("\n%d",*p1);//Comparison of two pointers (Pointers pointing to the same array)
p1++;
}
//Following are the invalid arithmetic operations(Not allowed on pointers)
//printf("\n%d",p1+p2);//Invalid arithmetic
//printf("\n%d",p1/p2);//Invalid arithmetic
//printf("\n%d",p1*p2);//Invalid arithmetic
//printf("\n%d",p1%p2);//Invalid arithmetic
//printf("\n%d",p1*2);//Invalid arithmetic
//printf("\n%d",p1/2);//Invalid arithmetic
//printf("\n%d",p1%2);//Invalid arithmetic
return 0;
}
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 Expression -Example
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *ptr1 = &a, *ptr2 = &b;
int *ptr3 = &b; // Using ptr3 to demonstrate *ptr1 + *ptr2 - *ptr3
printf("*ptr1 + *ptr2 = %d\n", *ptr1 + *ptr2);
printf("*ptr1 * *ptr2 = %d\n", *ptr1 * *ptr2);
printf("*ptr1 + *ptr2 - *ptr3 = %d\n", *ptr1 + *ptr2 - *ptr3);
printf("*ptr1 > *ptr2 = %d\n", *ptr1 > *ptr2);
printf("*ptr1 < *ptr2 = %d\n", *ptr1 < *ptr2);
int c = (*ptr1 > *ptr2) ? *ptr1 : *ptr2;
printf("c = %d\n", c);
(*ptr1)++;
Pointer Expression -Example
printf("After (*ptr1)++: a = %d\n", a);
(*ptr1)--;
printf("After (*ptr1)--: a = %d\n", a);
printf("*ptr1 & *ptr2 = %d\n", *ptr1 & *ptr2);
printf("*ptr1 | *ptr2 = %d\n", *ptr1 | *ptr2);
printf("*ptr1 ^ *ptr2 = %d\n", *ptr1 ^ *ptr2);
return 0;
}
Q1
Q2
Q3
Q4
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));
}
Example 1:Pointer to an array(1D)
#include <stdio.h>
int main()
{
int a[]={10,20,30,40,50};
int *ptr=&a[0];
int size=sizeof(a)/sizeof(a[0]);
for(int i=0;i<size;i++)
{
printf("%d ",*(ptr+i));
}
}
Example 2:Pointer to an array(1D)
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
int *ptr = arr;
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) {
sum += *(ptr + i);
}
printf(“%d”,sum);
return 0;
}
Example 3: Pointer to an array(1D)
#include <stdio.h>
int main() {
int arr[] = {10, 5, 3, 15, 2, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int *ptr = arr; // Pointer to the first element of the array
int min = *ptr; // Initialize min with the first element
// Using pointer to find the minimum element
for (int i = 1; i < size; i++) {
if (*(ptr + i) < min) {
min = *(ptr + i);
}
}
printf("Minimum element in the array: %d\n", min);
return 0;
}
Example 4: Pointer to an array(1D)
#include <stdio.h>
int main()
{
int a[5];
int *ptr=a;
for(int i=0;i<5;i++)
{
scanf("%d ",(ptr+i));
}
for(int i=0;i<5;i++)
{
printf("%d ",*(ptr+i));
}
}
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 vs Array
1) the sizeof operator
sizeof(array) returns the amount of memory used by all elements in array
sizeof(pointer) only returns the amount of memory used by the pointer variable itself
2) the & operator
&array is an alias for &array[0] and returns the address of the first element in array
&pointer returns the address of pointer
3) a string literal initialization of a character array
char array[] = “abc” sets the first four elements in array to ‘a’, ‘b’, ‘c’, and ‘\0’
char *pointer = “abc” sets pointer to the address of the “abc” string (which may be stored in read-
only memory and thus unchangeable)
4) Pointer variable can be assigned a value whereas array variable cannot be.
int a[10];
int *p;
p=a; /*legal*/
a=p; /*illegal*/
5) Arithmetic on pointer variable is allowed.
p++; /*Legal*/
a++; /*illegal*/
Passing Pointers to Function in C
Example: Passing Pointers to Function in C
#include <stdio.h>
void add(int *ptr)
{
(*ptr)++;
}
int main() {
int *p,i=12;
p=&i;
add(p);
printf("%d",*p);
return 0;
}
Q1
Solution
Q1

#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,i;
p++;
*(p+1)=29;
p=p+1;
*p=23;
p--;
*(p+0)=12;
p=a;
for(i=0;i<4;i++)
printf("%d ",*(p+i));
return 0;
}
A. 1 12 23 4
B. 1 29 23 12
C. 1 23 12 29
D. 1 23 3 4
Q1(Solution)

#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,i;
p++;
*(p+1)=29;
p=p+1;
*p=23;
p--;
*(p+0)=12;
p=a;
for(i=0;i<4;i++)
printf("%d ",*(p+i));
return 0;
}
A. 1 12 23 4
B. 1 29 23 12
C. 1 23 12 29
D. 1 23 3 4
Q2
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,i;
int *p2=&a[2];
p2--;
*(p2-1)=90;
p1=p2;
*p1=100;
for(i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}
A. 1 90 100 4
B. 90 100 3 4
C. 90 2 100 4
D. 1 2 90 100
Q2(Solution)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,i;
int *p2=&a[2];
p2--;
*(p2-1)=90;
p1=p2;
*p1=100;
for(i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}
A. 1 90 100 4
B. 90 100 3 4
C. 90 2 100 4
D. 1 2 90 100
Q3
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,*p2=&a[3];
p1++;
printf("\n%d %d",p2-p1,*p2-*p1);
return 0;
}
A. 2 2
B. 3 2
C. 3 3
D. 2 3
Q3(Solution)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,*p2=&a[3];
p1++;
printf("\n%d %d",p2-p1,*p2-*p1);
return 0;
}
A. 2 2
B. 3 2
C. 3 3
D. 2 3
Q4
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a;
*(p+1)=*(p+2);
printf("\n%d",a[2]);
return 0;
}
A. 3
B. 2
C. Compile time error
D. 0
Q4(Solution)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a;
*(p+1)=*(p+2);
printf("\n%d",a[2]);
return 0;
}
A. 3
B. 2
C. Compile time error
D. 0
Q5
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,x;
x=*p++;
printf("\n%d %d",x,*p);
return 0;
}
A. 1 2
B. 2 2
C. 1 1
D. Compile time error
Q5(Solution)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,x;
x=*p++;
printf("\n%d %d",x,*p);
return 0;
}
A. 1 2
B. 2 2
C. 1 1
D. Compile time error

You might also like