Pointers
Basics
1#include<stdio.h>
2int main()
3{
4 int a=10;
5 printf("Address of P: %p \n", *a);
6
7 int *p;
8
9 p=&a;
10
11 *p = 30;
12
13 printf("Address of P: %p \n", p);
14 printf("Value of P: %d \n", a);
15 printf("%d", *(&a));
16
17
18}
to read the elements and display
1#include<stdio.h>
2void rnp(int*p,int n);
3int main()
4{
5 int num,a[15];
6 printf("\n enter the number of elements:");
7 scanf("%d",&num);
8 rnp(a,num);
9
10}
11void rnp(int*p,int n)
12{
13 int i,sum=0;
14 int*q=p;
15 for(i=1;i<=n;i++)
16 {
17 printf("\nenter the %d elements of an array",i);
18 scanf("%d",p);
19 p++;
20 }
21 printf("\nelements are:");
22
23 for(i=1;i<=n;i++)
24 {
25 sum=sum+*q;
26 printf("%5d",*q);
27 q++;
28 }
29 printf("\nsum of elements of an array is %d",sum);
30 }
to display elements in reverse order
1#include <stdio.h>
2void display(int *p, int n);
3int main()
4{
5 int a[10], num, i;
6 printf("\nenter the number of elements:");
7 scanf("%d", &num);
8 printf("\nenter the elements:");
9 for (i = 0; i < num; i++)
10 scanf("%d", &a[i]);
11 display(a, num);
12}
13void display(int *p, int n)
14{
15 int i;
16 printf("\n elements in the array are:");
17 for(i=0;i<n;i++)
18 {
19 printf("%5d", *p);
20 p++;
21
22 }
23 p--;//it will take null character automatically at the end of the list whether you try to prevent it ,it will must take the garbage value.
24
25 printf("\neleemnts in the reverse order are:");
26 for(i=0;i<n;i++)
27 {
28 printf("%5d", *p);
29 p--;
30
31 }
32}