Sunday, 6 April 2014

Pointers

Pointers1
#include<stdio.h>
main()
{
int i=0x12345678;
int *p=&i;
printf("i=%x\n",i);
printf("i=%x\n",*(int *)p);
printf("i=%x\n",*(double *)p);
printf("i=%x\n",*(short int *)p);
char ch='B';
char *cp=&ch;
double d=12.5;
double *dp=&d;
printf("ch=%d at %u d=%f at %u\n",*cp,&ch,*dp,&d);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer1.c
pointer1.c: In function �main�:
pointer1.c:8:1: warning: format �%x� expects argument of type �unsigned int�, but argument 2 has type �double� [-Wformat=]
 printf("i=%x\n",*(double *)p);
 ^
pointer1.c:14:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �char *� [-Wformat=]
 printf("ch=%d at %u d=%f at %u\n",*cp,&ch,*dp,&d);
 ^
pointer1.c:14:1: warning: format �%u� expects argument of type �unsigned int�, but argument 5 has type �double *� [-Wformat=]
madan@madan-Lenovo-G570:~/madan$ ./a.out
i=12345678
i=12345678
i=7ffffff5
i=5678
ch=66 at 2484735611 d=12.500000 at 2484735616
pointers-2
#include<stdio.h>
main()
{
int choice,c;
printf("enter ur choice\n");
scanf("%d",&choice);
int a=10,b=20;
int *ipa=&a,*ipb=&b,*ipc=&c;
switch(choice)
{
case 1:
//c=a+b;
printf("c=%d at %u",*ipc=*ipa+*ipb,ipc);
break;
case 2:
printf("c=%d at %u\n",*ipc=*ipa-*ipb,ipc);
break;
default:printf("enter currect choice\n");
}
printf("\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer2.c
pointer2.c: In function �main�:
pointer2.c:13:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
 printf("c=%d at %u",*ipc=*ipa+*ipb,ipc);
 ^
pointer2.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
 printf("c=%d at %u\n",*ipc=*ipa-*ipb,ipc);
 ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter ur choice
1
c=30 at 2295102972
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter ur choice
2
c=-10 at 88970556
Pointers-3
#include<stdio.h>
main()
{
float f=12.5;
float *p=&f;
char c,*cp=&f;
int i,*ip=&f;
float g;
g=*p;
i=*ip;
c=*cp;

printf("g=%f\n",g);
printf("i=%d\n",i);
printf("c=%c\n",c);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer3.c
pointer3.c: In function �main�:
pointer3.c:6:12: warning: initialization from incompatible pointer type [enabled by default]
 char c,*cp=&f;
            ^
pointer3.c:7:11: warning: initialization from incompatible pointer type [enabled by default]
 int i,*ip=&f;
           ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
g=12.500000
i=1095237632
c=
Pointers to check binary Pattern
#include<stdio.h>
main()
{
float f=10.5;
char c,*cp=&f;
int i,n,j;
printf("enter value for n\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
c=*(cp+i);
for(j=7;j>=0;j--)
printf("%d ",(c>>j)&1?1:0);
printf("\n");
}
printf("\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer4.c
pointer4.c: In function �main�:
pointer4.c:5:12: warning: initialization from incompatible pointer type [enabled by default]
 char c,*cp=&f;
            ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
7
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 1 1 1 
0 1 0 0 0 0 0 1 
0 0 1 0 1 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0
Increment Operation Through pointers
#include<stdio.h>
main()
{
int a=10,b=15,c;
int *ip1=&a,*ip2=&b;
//a=*ip1;
//b=*ip2;
a=*ip1;
printf("a=%d\n",a);
a=(*ip1)++;
printf("a=%d\n",a);
//c=*ip1+*ip2;
//printf("%d %d %d\n",*ip1,*ip2,c);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer5.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
a=10
a=10

command line arguments printing
#include<stdio.h>
main(int n,char **argv)
{
int i;
printf("argc=%d\n",n);
for(i=1;i<=n;i++)
printf("argv[%d]=%s\n",i,argv[i]);
}
Output:
madan@madan-Lenovo-G570:~/madan$ cc pointer6.c
madan@madan-Lenovo-G570:~/madan$ ./a.out 12 34 54 65
argc=5
argv[1]=12
argv[2]=34
argv[3]=54
argv[4]=65
argv[5]=(null)

PointersExample
#include<stdio.h>
main()
{
int i,j,a[2][3];
printf("enter 2D array\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("a[%d][%d]=%d\n",i,j,a[i][j]);
printf("%u %u %u %d %d\n",a,*a,*a+1,a[0],a[0][2]);
printf("%u %u %u\n",*(a+1),*((*a+1)+1),**(a+1));
}
Output:
madan@madan-Lenovo-G570:~/madan$ cc pointer7.c
pointer7.c: In function �main�:
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �int (*)[3]� [-Wformat=]
 printf("%u %u %u %d %d\n",a,*a,*a+1,a[0],a[0][2]);  
 ^
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 4 has type �int *� [-Wformat=]
pointer7.c:16:1: warning: format �%d� expects argument of type �int�, but argument 5 has type �int *� [-Wformat=]
pointer7.c:17:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �int *� [-Wformat=]
 printf("%u %u %u\n",*(a+1),*((*a+1)+1),**(a+1));  
 ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 2D array
1
2
3
4
5
6     
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
642338032 642338032 642338036 642338032 3
642338044 3 4
Array Printing...
#include<stdio.h>
main()
{
char str[5][10],i,j;
printf("enter array elements\n");
for(i=0;i<5;i++)
scanf("%s",str[i]);
printf("resultant array is...\n");
for(i=0;i<5;i++)
printf("%s\n",str[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer8.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter array elements
well
come
madan     
hyderabad
india
resultant array is...
well
come
madan
hyderabad
india
2D_Array Passing by Using Pointers
#include<stdio.h>
void print(int (*p)[3],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
printf("%d\n",p[i][j]);
}
main()
{
int a[2][3]={{11,22,33},{44,55,66}};
print(a,2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer9.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
11
22
33
44
55
66