Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
W EEK -E ND A SSIGNMENT -07
Pointers, m-D array and Dynamic Memory Allocation
Operating Systems Workshop (CSE 3541)
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.
Instruction to Students (If any):
Students are required to write his/her own program by avoiding any kind of copy from any sources.
Additionally, They must be able to realise the outcome of that question in relevant to systems pro-
gramming. You may use additional pages on requirement.
Programming/ Output Based Questions:
1. Consider the following ANSI C program;
#include<stdio.h> What is the output of the above program?
int main(){
int arr[4][5];
Output with explanation
int i,j; (A) 14 (C) 24
for(i=0;i<4;i++){
for(j=0;j<5;j++){ (B) 20 (D) 30
arr[i][j]=10*i+j;
} (A) 24 (C) 14
}
(B) 30 (D) 20
printf("%d\n",arr[2][4]);
printf("%d\n",*(*(arr+2)+4));
return 0;
}
2. Consider the following ANSI C program; [GATE 2021]
#include<stdio.h> What is the output of the above program?
int main(){
int arr[4][5]; Output with explanation
int i,j;
(A) 14 (C) 24
for(i=0;i<4;i++){
for(j=0;j<5;j++){
arr[i][j]=10*i+j;
(B) 20 (D) 30
}
}
printf("%d\n",*(arr[1]+9));
return 0; ANS (C)24
}
[Assign7-1]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
3. What is printed by the following ANSI C program? [GATE 2022]
#include<stdio.h>
int main(void) Output with explanation
{
int x = 1, z[2] = {10, 11}; (A) 1, 10, 11 (C) 10, 14, 11
int *p = NULL;
p = &x; (B) 1, 10, 14 (D) 10, 10, 14
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3; ANS (D)10, 10, 14
printf("%d, %d, %d\n", x, z[0], z[1]);
return 0;
}
4. What is printed by the following ANSI C program? [GATE 2022]
#include<stdio.h>
int main(int argc, char *argv[])
{ Outputt
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9}, student@iteradmin-Vostro-3268:~/Documen
{10, 11, 12, 13, 14, 15, 16, 17, 18}, ts/2141016287_substitute$ ./a.out
{19, 20, 21, 22, 23, 24, 25, 26, 27}}; 123
int i = 0, j = 0, k = 0; 10 11 12
for( i = 0; i < 3; i++ ){ 19 20 21
for(k = 0; k < 3; k++ )
printf("%d ", a[i][j][k]);
printf("\n");
}
return 0;
}
5. What is printed by the following ANSI C program? [GATE 2022]
#include<stdio.h>
int main(int argc, char *argv[])
{ Outputt
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9}, ASCII encoding for relevant characters is given
{10, 11, 12, 13, 14, 15, 16, 17, 18}, below
{19, 20, 21, 22, 23, 24, 25, 26, 27}}; A B C ... Z
int i = 0, j = 0, k = 0; 65 66 67 ... 90
for( i = 0; i < 3; i++ ){ a b c ... z ∗ + -
for(k = 0; k < 3; k++ ) 97 98 99 ... 122 42 43 45
printf("%d ", a[i][j][k]);
printf("\n"); (A) z K S (C) * - +
}
(B) 122 75 83 (D) P x +
return 0;
} ANS (B)122 75 83
6. Consider the program below; [GATE 2009]
[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
}
7. Consider the following C program [GATE 2020]
#include<stdio.h>
int main()
{
int a[4][5]={
{1,2,3,4,5}, The output of the program is
{6,7,8,9,10},
{11,12,13,14,15},
Outputt
{16,17,18,19,20}}; student@iteradmin-Vostro-3268:~/Documents
printf("%d\n",*(*(a+**a+2)+3)); /2141016287_substitute$ ./a.out
return 0; 19
}
8. Consider the following C function; [GATE 2020]
int tob(int b, int *arr){ int pp(int a, int b){
int i; int arr[20];
for(i=0;b>0;i++){ int i,tot=1,ex,len;
if(b%2) ex=a;
arr[i]=1; len=tob(b,arr);
else for(i=0;i<len;i++){
arr[i]=0; if(arr[i]==1){
b=b/2; tot=tot*ex;
} ex=ex*ex;
return(i); return(tot);
} }
The value returned by pp(3,4) is
Write the execution pattern and final outputt
(i) len:
(ii) arr content:
(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
9. Write the output of the following program;
#include<stdio.h> void fun(int *p,int *q){
void fun(int *,int *); p=q;
int main() *q=10;
{ }
int i=5,j=5;
fun(&i,&j); Outputt
printf("%d %d\n",i,j);
return 0; student@iteradmin-Vostro-3268:~/Documents
} /2141016287_substitute$ ./a.out
5 10
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
13. Select the output of the following program.
ASCII value of a=97 and b=98
int main(){
int a[][3]={4,5,6,7,8,9,1,2,3}; Outputt
printf("%d,", *a[2]);
printf("%d,", a[2][0]); (A) 1024,1,1 (C) 1024,2,1024
printf("%d ", **(a+1+(’b’-’a’)));
return 0; (B) 1,1,1 (D) None of these
}
ANS (B)1,1,1
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
16. Select the output of the following program..
int main(){
int *ptr; Outputt
ptr=(int *)realloc(NULL,sizeof(int)); student@iteradmin-Vostro-3268:~/Document
*ptr=100; s/2141016287_substitute$ ./a.out
printf("%d\n",*ptr); 100
return 0;
}
17. Write the output of the following program.
1 int main(){int *ptr;
2 ptr=(int *)calloc(1,sizeof(int)); Outputt
3 *ptr=100;
4 printf("%d\n",*ptr); Output at line-4:
5 ptr=(int *)realloc(ptr,0);
Output at line-7:
6 ptr=NULL;
7 printf("%p\n",ptr); Line number-6 can be treated as like free() to
8 return 0;} deallocate memory-Y|N.
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
19. Select the output of the following program.
int main(){
int b=65; Output t
void *p=&b;
int *j=(int *)p; ANS (B) 65 A
(A) 65 65 (C) Compile time
char *ch=(char *)p; error
printf("%d %c\n",*j,*ch);
return 0; (B) 65 A (D) Run time error
}
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
23. Which of the following statements are true?.
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
24. Check the error or output of the following program?
int main(){ (i) 20 20 20
void *p; (ii) 20 30 20 Outputt
int *i=20; (iii) compile error
p=&i; at line-4
void *q=p; //line-4 (iv) compile error
//line-5 at line-5
printf("%d %d %d\n",i,*p,*q);
}
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;
}
26. State the output of the code.
#include<stdio.h> int main(){
Outputt
int f(int n){ int (*p)(int)=f; student@iteradmin-Vostro-3268:~/Docume
while(--n>=0){ (*p)(8); nts/2141016287_substitute$ ./a.out
printf("%d ",n-2); return 0; 5 4 3 2 1 0 -1 -2
} }
return 1;
}
27. Write the output of the given code snippet.
#include<stdio.h> #include<stdio.h>
int main(){ void demo(){
void demo(); printf("SS");
Outputt
void (*fun)(); } student@iteradmin-Vostro-3268:~/Documen
fun=demo; ts/2141016287_substitute$ ./a.out
(*)fun(); SS
fun();
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
33. Find the output of the code snippet.
Outputt
int main(){ student@iteradmin-Vostro-3268:~/Document
int a[][2][4]={5,6,7,8,9,11,12,1}; s/2141016287_substitute$ ./a.out
printf("%d\n",*(*(*(a+0)+1)+2)); 12
return 0;
}
[Assign7-8]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
34. Describe the output for the following code snippet.
void fun(int arr[][3]){
printf("%d\n",*(*(arr+2)+1)); Outputt
printf("%p\n",(*arr)+2);
printf("%p\n",&arr[0][2]);
student@iteradmin-Vostro-3268:~/Documents/
printf("%d\n",*(((*arr)+1)+1));
2141016287_substitute$ ./a.out
} 2
int main(){ 0x7fff67873048
int a[][3]={5,6,7,8,9,4,3,2,1}; 0x7fff67873048
fun(a); 7
return 0;
}
35∗. Explain the below declaration(s).
(1) int process(int (*pf)(int a, int b)) ;
(2) int (*fun(int, void (*ptr)()))();
(3) int *(*p)(int (*a)[ ]);
(4) int (*p)[10];
(5) float *p[20];
(6) int p(char *a);
(7) int (*p(char * a))[l0];
(8) int * (*p [10]) (char *a);
................
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]