Find the output of following c program
1. #include<stdio.h>
int main()
{
int a=11,*ptr;
ptr=&a;
printf("Enter a number ");
scanf("%d",ptr); /*Suppose the input number is
16*/
printf ("%d %d\n",a,*ptr);
return 0;
}
2. #include<stdio.h>
int main()
{
int *ptr;
printf ("Enter a number :");
scanf("%d",ptr) ;
printf("%d\n",*ptr) ;
return 0;
}
3. #include<stdio.h>
int main()
{
int arr[4],i;
for(i=0;i<4;i++)
printf("%u ",arr+i); /*Suppose base address is 8000, and int
size 4 byte*/
return 0;
}
4.#include<stdio.h>
int main()
{
int arr[4],i;
printf("Enter 4 numbers\n");
for(i=0;i<4;i++)
scanf("%d",arr+i);
for(i=0;i<4;i++)
printf("%d ",*(arr+i));
return 0;
}
5. #include<stdio.h>
int main()
{
int i,arr[3]={11,22,33},*p;
p=&arr;
for(i=0;i<=3;i++)
printf("%d %d\t",*(p+i) ,p[i]);
return 0;
}
6. #include<stdio.h>
int main()
{
int i,arr[4]={11,22,33,44},*p;
p=&arr[4];
for(i=0;i<4;i++)
printf("%d ",*arr);
arr++;
return 0;
}
7. #include<stdio.h>
int main()
{
int i,arr[4]={2,4,7,12},*p=arr;
for(i=0;i<4;i++)
{
(*p)++;
printf("%d ",*p);
p++;
}
return 0;
}
8. #include<stdio.h>
int main()
{
int i,arr[4]={2,4,7,12},*p=arr;
for(i=0;i<4;i++,p++)
printf("%d ",(*p)++);
return 0;
}
9. #include<stdio.h>
int main()
{
int i,arr[4]={2,4,7,12},*p=arr;
for(i=0;i<4;i++)
printf("%d ",(*p)++);
p++;
return 0;
}
10.#include<stdio.h>
int main()
{
int i,arr[4]={2,4,7,12},*p=arr;
for(i=0;i<4;i++,p++)
printf("%d ",*p++);
return 0;
}
11.include<stdio.h>
int main()
{
int i,arr[4]={11,22,33,44},*p=arr;
for(i=0;i<4;i++)
printf("%d ",*p++);
printf("\n");
for(i=0;i<4;i++)
printf("%d ",*--p);
printf ("\n");
return 0;
}
12.#include<stdio.h>
int main()
{
int i,arr[4]={2,4,7,12},*p=&arr[3];
for(i=0;i<4;i++)
printf("%d ",*--p);
return 0;
}
13.#include<stdio.h>
int main()
{
int i,arr[4]={11,22,33,44},*p=arr;
for(i=0;i<4;i++)
printf("%d ",*p++);
printf("\n");
for(i=0;i<4;i++)
printf("%d ",*p--);
printf ("\n");
return 0;
}
14. #include<stdio.h>
int main()
{
int i,arr[4]={11,22,33,44},*p=arr;
for(i=0;i<4;i++)
printf("%d ",*++p);
printf("\n");
for(i=0;i<4;i++)
printf("%d ",*--p);
printf ("\n");
return 0;
}
15.#include<stdio.h>
int main()
{
int i,arr[4]={11,22,33,44},*p=arr;
for(i=0;i<4;i++)
printf("%d ",++*p);
printf("\n");
for(i=0;i<3;i++)
printf("%d ",(*p)++);
printf("\n");
return 0;
}
16.#include<stdio.h>
int main()
{
int arr[]={3,6,9,12,15},*p;
for(p=&arr[0];p<arr+5;p++)
printf("%d ",*p);
return 0;
}
17.#include<stdio.h>
int main()
{
int arr[10]={5,10,15,20,25},*p ;
for(p=arr+2; p<arr+8; p=p+2) ;
printf("%d ",*p);
return 0;
}
18.#include<stdio.h>
int main()
{
int arr[4]={5,10,15,20},*p;
for(p=arr+1;p<arr+3;p=p+2)
printf("%d ",*p);
return 0;
}
19.#include<stdio.h>
int main()
{
int i,arr[4]={2,3,4,5};
int *p=arr+3;
for(i=0;i<4;i++)
printf("%d ",*p--);
return 0;
}
20.#include<stdio.h>
int main()
{
int arr[4]={1,2,3,4};
int x = (arr+1)[1];
printf ("%d\n", x);
return 0;
}
21.#include<stdio.h>
int main()
{
int arr[4]={11,22,33,44};
int *p,*q;
p=q*2;
printf("%d %d",*p,*q) ;
return 0;
}
22.#include<stdio.h>
int main()
{
int arr[6]={2,3,4,5,6} ;
int *ptr=arr;
printf ("Size of ptr=%u Size of
arr=%u",sizeof(ptr),sizeof(arr));
return 0;
}
23.#include<stdio.h>
int main()
{
float a=5,*p,**pp;
p=&a;
pp=&p;
printf("sizeof a:%d sizeof p:%d sizeof pp:%d\n",sizeof(a),sizeof(p),sizeof(pp))
;
return 0;
}
24.#include<stdio.h>
int main()
{
float a=5,*p,**pp;
p=&a; /*Assume address of a is 5000*/
pp=&p; /*Assume address of p is
5500*/
printf("a=%f,p=%u,pp=%u\n",a,p,pp)
;
a=a+1;
p=p+1;
pp=pp+1;
printf("a=%f,p=%u,pp=%u\n",a,p,pp);
return 0;
}
25.#include<stdio.h>
void func (int x, int *y);
int main()
{
int a=2,b=6;
func(a,&b) ;
printf("a=%d,b=%d\n",a,b) ;
return 0;
}
void func(int x,int *y)
{
int temp;
temp=x;
x=*y;
*y=temp;
}
26.#include<stdio.h>
void change(int *p);
int a=5;
int main()
{
int x=20,*ptr=&x;
printf("%d ",*ptr);
change(ptr);
printf("%d ",*ptr);
return 0;
}
void change (int *p)
{
p=&a;
}
27.#include<stdio.h>
void change(int **pp);
int a=10;
int main()
{
int x=20,*ptr=&x;
printf("%d ",*ptr);
change(&ptr) ;
printf("%d\n",*ptr);
return 0;
}
void change(int **pp)
{
*pp=&a;
}
28.#include<stdio.h>
void func();
int *ptr;
int main ()
{
func();
printf("%d\n",*ptr);
return 0;
}
void func()
{
int
num=10;
ptr=#
}
29.#include<stdio.h>
void func(int,int);
int *ptr;
int main()
{
int
a=5,b=8;
func(a,b) ;
printf
("a=%d b=%d\n" , a, b) ;
return
0;
}
void func (int x, int y)
{
int
temp;
temp=*(&x)
,*(&x)=*(&y) ,*(&y)=temp;
}
30.#include<stdio.h>
void func(int*);
int *ptr;
int main()
{
int arr[5] ={1,2,3,4,5}; //Assume that base address of arr is
8000
int *p=arr;
printf("p %u,\t",p);
func(p);
printf("p %u",p);
return 0;
}
void func(int *ptr)
{
ptr++;
}
31.#include<stdio.h>
void func(int**);
int *ptr;
int main()
{
int arr[5] ={1,2,3,4,5}; //Assume that base address of arr is
8000
int *p=arr;
printf("p %u,\t",p);
func(&p);
printf("p %u\n",p);
return 0;
}
void func(int **pptr)
{
(*pptr)++;
}
32.#include<stdio.h>
func(int a[])
{
int b[10] ;
int x=5,y=4;
a=&x;
b=&y;
}
int main()
{
int arr[10];
func(arr) ;
return 0;
}
33.#include<stdio.h>
int main()
{
int arr[3][4]; /*Assume base address of arr is
5000*/
printf("%u %u
%d",arr,&arr[0][1],sizeof(arr[1][2]));
return 0;
}
34.#include<stdio.h>
int main()
{
int
i,arr[3][4]={{1,2,3,4,},{4,5,6,},{7,8,9}};
int *p=arr;
for(i=0;i<12;i++)
printf("%d ",p[i]);
return 0;
}
35.#include<stdio.h>
void func(int a[])
{
int i;
for(i=0;a[i]!=8;i++)
printf("%d ",a[i]);
}
int main()
{
{
int
arr[10]={1,2,3,4,5,6,7,8,9,10};
func(arr+3);
return
0;
}
}
36.#include<stdio.h>
int main()
{
int
i,arr[3][4] ={{2,3,4},{0},{7,9,8,9}};
int *ptr[3];
int (*p)[4];
p=arr;
for(i=0;i<3;i++)
ptr[i]=arr[i];
printf("%d
%d\n",ptr[0][0],ptr[2][3]);
printf("%d %d",p[0][0],p[2][3]);
return 0;
}
ANSWER
1.
16
16
2.
Run
time error
Hint-wild
pointer
3.
8000
8004 8008 8012
4.
it
will print four entered value
5.
it
will give a warning and after execution
11
11 22 22 33 33 gv gv
6.
compilation
error
7.
3
5 8 13
8.
2
4 7 12
9.
2
3 4 5
10. 2 7 gv gv
11. 11 22 33 44
44
33 22 11
12. 7 4 2 gv
13. 11 22 33 44
gv
44 33 22
14. 22 33 44 gv
44
33 22 11
15. 12 13 14 15
15
16 17
16. 3 6 9 12 15
17. gv
18. 10
19. 5,4,3,2
20. 3
21. compilation error
22. Size of ptr=8 Size of arr=24
23.
sizeof a:4 sizeof p:8 sizeof pp:8
24.
a=5.000000,p=5000,pp=5500
a=6.000000,p=5004,pp=5508
25.
a=2,b=2
26.
20 20
27.
20 10
28.
10 (hint-you may get the answer 10 but it is Dangling pointer)
29.
a=5 b=8
30.
p 8000, p
8000
31.
p 8000, p
8004
32.
compilation error
33.
5000 5004 4
34.
1 2 3 4 4 5 6 0 7 8 9 0
35.
4 5 6 7
36.
2 9
2 9
No comments:
Post a Comment
For any doubt or suggestion you can write here