1 Pointer Code Snippets
1 Pointer Code Snippets
1 Pointer Code Snippets
h>
int main()
int a[]={1,2,3,4,5};
int *p;
p=a;
printf("%d",*p+1);
return 0;
o/p:2
2) #include <stdio.h>
int main()
int a=5;
int *ptr;
ptr=&a;
*ptr=*ptr*3;
printf("%d",a);
return 0;
o/p:15
3) #include<stdio.h>
void main()
int i=6,*j,k;
j=&i;
printf("%d\n",i * *j*i+*j);
4) #include<stdio.h>
void main()
int x=10;
int *y,**z;
y=&x;
z=&y;
printf("x=%d\ny=%d\nz=%d\n",x,*y,**z);
void main()
int x=20,*y,*z;
y=&x;
z=y;
y++;
z++;
x++;
printf("x=%dy=%uz=%u",x,y,z);
#include<stdio.h>
void main()
int i,n,pos=-1,*a,element;
scanf("%d",&n);
a=malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf("%d",a+i);
scanf("%d",&element);
for(i=0;i<n;i++)
if(*(a+i)==element)
pos=i;
break;
printf("%d is in %d",element,pos);
}
7)program to accept 5 integer values from keyboard and print the element of array in reverse order
using a pointer
#include<stdio.h>
main()
int a[5];
int *p=a;
for(i=0;i<5;i++)
scanf(“%d”,p+i);
for(i=4;i>=0;i--)
Printf(“%d\n”,*(p+i);
#include<stdio.h>
void main()
int a[]={4,2,3,1};
int *p,i,j,t;
p=a;
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
if(*(p+j)<(*p+i))
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
for(i=0;i<4;i++)
printf("%d",*(p+i));
10)
#include<stdio.h>
void main()
int b[]={1,2,3,4,5};
int i,*k;
k=&b[4]-4;
for(i=0;i<=4;i++)
printf("%d",*k);
k++;
o/p:1 2 3 4 5
11.
#include<stdio.h>
void main()
int n[25];
n[0]=100;
n[24]=200;
printf("\n%d\n%d",*n,*(n+24)+*(n+0));
int main()
int a=10;
float b=1.2;
void *p;
p=&a;
printf("%d\n",*((int *)p));
p=&b;
printf("%f\n",*((float *)p));
return 0;