0% found this document useful (0 votes)
9 views

Module 4 Pointers

Uploaded by

lyricaltube6247
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 4 Pointers

Uploaded by

lyricaltube6247
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Write a C program to add two numbers using

pointers
#include<stdio.h>
int main()
Output:
{
int a, b, sum; Enter the value of a & b
int *p,*q; 10
20
printf(“Enter the value of a & b\n”);
scanf(“%d%d”,&a,&b); Sum=30
p=&a;
q=&b;
sum=*p+*q;
printf(“Sum=%d\n”, sum);
}
Pointer increment and
Decrement
Increment operator when used with a pointer variable returns next address pointed by
the pointer. The next address returned is the sum of current pointed address and size of
pointer data type.
Similarly, decrement operator returns the previous address pointed by the pointer. The
returned address is the difference of current pointed address and size of pointer data
type.
int num = 5;
int *ptr;

ptr = &num;
ptr++;
ptr--;
Pointer athematic
Pointer addition
The final value of the pointer variable can be computed using:

Final value=Current value+(integer number * sizeof(datatype));


Example:
int main() Output:
{ Value before adding= 24971
int x; Value after adding=24991
int *p; Final value=Current value+(integer number * sizeof(datatype));
p=&x; = 24971+(10*sizeof(int));
printf(“Value before adding= %u”, p); =24971+(10*2)
=24971+20
p=p+10;
=24991
printf(“Value after adding= %u”, p);
}
Pointer subtraction
The final value of the pointer variable can be computed using:

Final value=Current value - (integer number * sizeof(datatype));


Example:
Output:
int main() Value before subtraction = 24971
{ Value after subtraction =24951
int x;
Final value=Current value - (integer number *
int *p; sizeof(datatype));
p=&x; = 24971-(10*sizeof(int));
=24971-(10*2)
printf(“Value before subtraction= %u”, p); =24971-20
p=p-10; =24991

printf(“Value after subtraction = %u”, p);


}
ARRAY & POINTERS
Pointer comparison
The two pointers can be compared if both the pointers are pointing to same datatype
Ex:
int a[5]={10,20,30,40,50}; a[0] a[2] a[2] a[3] a[4]
int *ptr1,*ptr2;
10 20 30 40 50
float *fptr;
ptr1=a;
ptr2=&a[4];
ptr1!=ptr2;
ptr1==ptr2;
ptr1<=ptr2;
fptr!=ptr1;
#include<stdio.h>
Int main() a[0] a[2] a[2] a[3] a[4]

{ 5 10 15 20

int []={5,10,15,20};
int *p,*q;
p=a[0];
q=a[3];

while(p<=q)
{
printf(“%d\t”,p);
p++;
Output:
}
5 10 15 20
}
Develop a program using pointers to compute the sum,
mean and standard deviation of all elements stored in
an array of n real numbers.
Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers.

Mean= Sum of all elements / no of elements

standard deviation
Develop a program using pointers to compute the
sum, mean and standard deviation of all elements
stored in an array of n real numbers.

int main()
a[0] a[1] a[2]
{
float a[10],*ptr, mean, std, sum=0,sumstd=0; 1 2 3 4 5
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
a[0] a[1] a[2]
{
sum=sum+*ptr; 1 2 3 4 5
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd = sumstd + pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation =%.3f\t",std)
}
OUTPUT:
.....................................................................
Enter the number of elements
5
Enter array elements
1
2
3
4
5
Sum=15.000
Mean=3.000
Standard Deviation =1.414

You might also like