Module 4 Pointers
Module 4 Pointers
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 = #
ptr++;
ptr--;
Pointer athematic
Pointer addition
The final value of the pointer variable can be computed using:
{ 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.
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