WeekEndAssignment 7 (FINAL)
WeekEndAssignment 7 (FINAL)
Problem Statement:
Experiment with pointers, 1-D & m-D array processing through pointers and dynamic memory allocation
in C.
Assignment Objectives:
To learn how to manipulate arrays using pointers and to learn malloc, mcalloc, realloc & free to
allocate and free dynamic memory.
[Assign7-1]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
[Assign7-2]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
int main(){
#include<stdio.h> int x=15;
int fun(int n, int *f_p){ printf("%d\n",fun(5,&x));
int t,f; return 0;
if(n<=1){ }
*f_p=1; The value printed is
return 1;
} Outputt
t=fun(n-1, f_p);
f=t+ *f_p; (A) 6 (C) 14
*f_p=t;
return f; (B) 8 (D) 15
ANS (B)8
}
(i) len:
(iii) tot:
(iv) ex:
Finally, pp(3,4):
[Assign7-3]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
10. Find the output and different types of pointer involved in the code snippet;
int main(){
int *p=NULL; Outputt
p=(int *)malloc(sizeof(int));
*p=10; student@iteradmin-Vostro-3268:~/Docume
free(p); nts/2141016287_substitute$ ./a.out
int *q; 15 15
q=(int *)malloc(sizeof(int));
*q=15;
printf("%d %d\n",*p,*q);
return 0;
}
11. State the output of the following program. Assume the address of p is 1000 and q is 2000.
#include<stdio.h> void fun(int **q){
#include<stdlib,h> int r=20;
void fun(int **q); **q=r;
int main(){ printf("%p\n",*q);
int *p=(int *)malloc(sizeof(int)); }
*p=55;
fun(&p); Outputt
printf("%d %p\n",*p,p); student@iteradmin-Vostro-3268:~/Document
return 0; s/2141016287_substitute$ ./a.out
} 0x55f3a35082a0
20 0x55f3a35082a0
12. Write the output of the code snippet by observing the co-relation of pointer manipulation in 2-D array.
int main(){
int n=4,m=3; Outputt
int a[n][m];
int (*p)[m]=a; (A)1
p=p+1;
(*)p[2]=100; (B)100
n=p-a;
printf("%d\n",n); /*----(A) */ (C)100
printf("%d\n",(*p)[2]); /*----(B) */
printf("%d\n",*((*p)+2));/*----(C) */ (D)0
printf("%d\n",*(a[1]+2));/*----(D) */
printf("%d\n",*(*p+2)); /*----(E) */ (E)100
printf("%d\n",*(p[0]+2));/*----(F) */
return 0;
(F)100
}
[Assign7-4]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
14. Select the desire output of the following code snippet with reason;
int fun(); int fun(){
Output with reasont
* *
int main(void){ int a=10,b=20; (A) Unexpected be- (C) 30
int *ptr; int sum=0; havoir
ptr=fun(); sum=sum+a+b;
printf("%d\n",*ptr); return ∑ (B) Address of sum (D) None of these
return 0; } ANS (A)Unexpected behavior
}
15. Select the desire output of the following code snippet with reason;
int *fun(); int *fun(){ Output with reasont
int main(void) int a=10,b=20,*sum;
{ sum=(int *)malloc( (A) Unexpected be- (C) 30
int *ptr=fun(); sizeof(int)); havoir
printf("%d\n",*ptr); *sum=a+b;
return 0; return sum; (B) Address of sum (D) None of these
} } ANS(A) Unexpected behavior
Observationt
int main(){int b=65; void p=b;printf("%d",p); student@iteradmin-Vostro-3268:~/Document
return 0;} s/2141016287_substitute$ ./a.out 65
18.
[Assign7-5]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
20. Write the output of the code snippet. Also show the stack and heap memory for this application.
Output t
int main(){int i;
int *p=(int *)malloc(sizeof(int)); 10...10 Heap:
20...20 |------------------|
*p=100;
p=(int *)malloc(5*sizeof(int)); 30...30 | [ 100 ] | <- Memory
40...40 allocated for the first integer
for(i=0;i<5;i++){
50...50 |------------------|
scanf("%d",p+i); /* 10,20,30,40,50 */
| [ 10 | 20 | 30 | 40 | 50 ] | <-
}
Memory allocated for the array of
for(i=0;i<5;i++){
Stack: five integers
printf("%d...%d\n",p[i], *(p+i)); int i |------------------|
} int *p
return 0;}
21. Write the output of the code snippet. Also show the stack and heap memory for this application.
Output
int main(){ 10...10 t Heap:
int i,*p,*rp; 20...20 |--------------------------|
p=(int *)malloc(5*sizeof(int)); 30...30 | [ 10 | 20 | 30 | 40 | 50 ]
for(i=0;i<5;i++) 40...40 | <- Original memory block
scanf("%d",p+i); /* 10,20,30,40,50 */ 50...50 (resized in the next step)
rp=(int *)realloc(p,10*sizeof(int)); 9...9 |--------------------------|
for(i=5;i<10;i++) 8...8 | [ 9 | 8 | 6 | 5 | 4 | ... ]
scanf("%d",rp+i);/* 9,8,6,5,4 */ 6...6 | <- Extended memory block
for(i=0;i<10;i++){ 5...5 after realloc
printf("%d...%d\n",rp[i],*(rp+i)); 4...4 |--------------------------|
} Stack:
return 0;} int i,
int *p ,int *rp
22. Which of the given statements about the following code snippet is/are correct?
(i) p is a wild pointer
(ii) r is a NULL pointer
(iii) q is dangling pointer
(iv) p is dangling pointer
void fun(){ (v) fun() is making memory leak
int *q=(int *)malloc(sizeof(int));
*q=20; Output t
}
So, the correct statements are:
int main(){
int *p;
r is a NULL pointer.
int *r=NULL; q is a dangling pointer.
fun(); fun() is making a memory leak.
return 0;
}
[Assign7-6]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Outputt
(1) (void *)0 is a void pointer (1)true
(2) (void *)0 is a NULL pointer (2)true
(3) int *p=(int *)0; p is a NULL pointer (3)true
(4) a[i]==i[a] (4)true
(5) a[i][j]== *(*(a+i)+j) (5)true
25. Write the output of the following program? Assume that the base address of a given array a is 1000?
Outputt
int main(){
int a[3][3]={4,5,6,7,8,9,1,2,3}; student@iteradmin-Vostro-3268:~/Documents
printf("%p %p %p\n",a[1]+2,*(a+1)+2,&a[1][2]) /2141016287_substitute$ ./a.out
; 0x7fff46e70a84 0x7fff46e70a84 0x7fff46e70
printf("%d %d %d\n",*(a[1]+2),*(*(a+1)+2), a a84
[1][2]); 999
return 0;
}
28. Write the output of the given code snippet that uses pointer to function or function pointer.
int fun(int x,int y){
int z=x+y+x*y;
return z;
}
[Assign7-7]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
#include<stdio.h>
int main(){
Outputt
int (*fun_ptr)(int,int); student@iteradmin-Vostro-3268:~/Documen
fun_ptr=fun; ts/2141016287_substitute$ ./a.out
int x=fun_ptr(34,56); 1994
printf("%d\n",x);
return 0;
}
29. Mention the output of the following code snippet. [Array of pointers to function returning int type].
#include<stdio.h> int fun1(int x,int y){
int main(){ return x+y; Outputt
int x,y; }
int (*fun_ptr[2])(int,int);
9...20
fun_ptr[0]=fun1; int fun2(int x,int y){
x=fun_ptr[0](4,5);; return x*y;
fun_ptr[1]=fun2; }
y=(*fun_ptr[1])(4,5);
printf("%d...%d\n",x,y);
return 0;
}
30. Find out the correct syntal(s) for making a constant pointer (i.e. The value of the pointer is constant
and pointer cannot be modified).
Outputt
(1) const <data_type> * ptr; const <data_type> * ptr;
(2) <data_type> * const ptr;
(3) <dat_type> const *ptr;
(4) <data_type> const * const fun_ptr
(5) None of these
31. Find out the correct syntal(s) for a pointer to constant (i.e. The pointer cannot able to change the value
of the variable/array that it points).
Outputt
(1) const <data_type> * ptr; const <data_type> * ptr;
(2) <data_type> * const ptr;
(3) <dat_type> const *ptr;
(4) <data_type> const * const fun_ptr
(5) None of these
32. Select the correct way of declaring and initializing pointer to function (i.e. function pointer).
Outputt
(1) int (*ptr)(int,int,int)=funname; int (*ptr)(int, int, int) = &funname;
(2) int *ptr(int,int,int)=funname;
(3) int (*ptr)(int,int,int)=&funname;
(4) (int *) ptr(int,int,int)=funname;
(5) None of these
[Assign7-8]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Outputt
(1)The function takes a single argument, which is a pointer to a function (pf) that takes two int parameters
(a and b) and returns an int.
(2)It takes two parameters: an int and a pointer to a function that takes no parameters (void (*ptr)())
and returns void.The fun function itself returns a pointer to a function that takes no parameters and
returns an int.
(3)p is a pointer to a function that takes a pointer to an array of integers (int (*a)[]) and returns
a pointer to an integer (int *).
(4)This declares p as a pointer to an array of 10 integers.
(5)This declares an array p of 20 elements, where each element is a pointer to a float. So, p is an
array of 20 float pointers.
(6)This is a function prototype for a function named p that takes a single argument, which is a pointer
to a character (char *a), and returns an int.
(7)This declares p as a function that takes a pointer to a character (char *a) as an argument and returns
a pointer to an array of 10 integers.
(8)declares an array p of 10 elements, where each element is a pointer to a function. Each function takes
a pointer to a character (char *a) as an argument and returns a pointer to an integer (int *).
[Assign7-9]