Pointers
1. Which of the following are valid pointer declarations.
1. int *Int;
Ans: Valid.
2. int *float = NULL;
Ans: Invalid. float is a keyword and cannot be used as a variable name.
3. char *cptr = NULL;
Ans: Valid.
2. Which of the following statements are true
1. A pointer to float can store a float
Ans: False. A pointer to anything can store a memory address.
2. A pointer is a variable that holds address of another variable.
Ans: True.
3. It is possible to declare an int and a pointer to int as part of same declaration statement.
Ans: True. int *ptr = NULL, val = 0;
4. It is possible to declare an float and a pointer to double as part of same declaration statement.
Ans: False. Only one data type is allowed in one declaration statement.
3. What will be printed by code snippet shown below
int var1=7, var2=10; int *ptr1=&var1, *ptr2=&var2; printf("var1 = %d var2 = %d\n",*ptr1,*ptr2);
Ans: var1 = 7 var2 = 10
4. What will be printed by code snippet shown below.
int var1=7, var2=10; int *ptr = NULL; ptr = &var1; var2 += *ptr; ptr = &var2; var1 += *ptr; printf("var1 = %d var2 = %d\n", var1, var2);
Ans: var1 = 24 var2 = 17
5. What will be the output of following program.
#include <stdio.h> int silly_function(int *, int *); int main (int argc, char* argv[]) { int num1=11, num2=13; num1 = silly_function(&num1, &num2); printf("%d %d\n",num1, num2); return 0; } int silly_function(int *p1, int *p2){ int num1=3, num2=5; num1 += *p1; num2 -= *p2; return num1+num2; }
Ans: 6 13
6. Extend the above program to write an integer calculator for operations add, subtract, multiply, and divide. Each operation should be implemented as a separate function. All values should be passed to functions using pointers.
01: #include <stdio.h> 02: int add(int *a, int *b); 03: int main (int argc, char * argv[]) { 04: int var1=7, var2=10, sum=0; 05: int *ptr1 = &var1, *ptr2 = &var2; 06: sum = add(ptr1, ptr2); 07: printf("%d + %d = %d\n", var1, var2, sum); 08: return 0; 09: } 10: int add(int *a, int *b) { 11: return(*a + *b); 12: }
int subtract(int *a, int *b) { return(*a - *b); } int multiply(int *a, int *b) { return(*a * *b); } int divide(int *a, int *b) { return(*a / *b); }
7. Which of the following statements are true.
1. A function cannot have some of its parameters passed by value and others passed by reference.
Ans: False.
2. Called function can modify the value of variables defined in calling function if those variables have been passed by value to the called function.
Ans: False. Values can be modified only if they have been passed by address.
3. Size of pointer to double > size of pointer to char.
Ans: False. Size of all pointers is the same, they all hold memory address.
8. Write a program to print size of pointer to double and pointer to char.
#include <stdio.h> int main (int argc, char * argv[]) { printf("%lu %lu\n", sizeof(double *), sizeof(char *)); return 0; }
9. Write a function to swap two integers using pointers.
#include <stdio.h> void swap(int *a, int *b) { int temp = 0; temp = *a; *a = *b; *b = temp; } int main (int argc, char * argv[]) { int a = 10, b = 20; swap(&a, &b); printf("a = %d and b = %d", a, b); return 0; }
10. Write a function that takes an int pointer as parameter and updates the value at that location to next highest perfect square. For example, if parameter was pointer to value 17 then function should update it to 25.
int next_highest_perfect_square(int val) { int i = 1; while(i*i <= val) { i++; } return i*i; } void update_val(int *a) { *a = next_highest_perfect_square(*a); }
11. Trace control flow of above example of output parameters.
#include <stdio.h> int area_and_circumference(float radius, float *area, float *circumference); int main (int argc, char * argv[]) { float radius = 3.1; float area = 0, circumference = 0; int status = -1; status = area_and_circumference(radius, &area, &circumference); if (status>0) { printf ("area=%f circumference=%f\n", area, circumference); } else { printf("Error computing area and circumference\n"); } int area_and_circumference(float radius, float *area, float *circumference) { if (radius < 0) { return 0; } *area = 3.14 * radius * radius; *circumference = 3.14 * 2 * radius; return 1; }
12. Write a function that takes an int n as parameter, returns sum of first n natural numbers, and provides square of n as output parameter.
int sum_and_square(int val, int *sum) { *sum = (val * (val+1))/2; return val*val; }
13. Which of the following statements are true.
1. An output parameter is declared in called function.
Ans: False. Output parameter is declared in calling function.
2. An output parameter can return address of a variable local to called function.
Ans: False. While program will compile fine, behaviour will be undefined. Local variable goes out of scope upon return. As soon as we access an out of scope variable using its address, results are undefined.
14. What will be the output of below program.
#include <stdio.h> int accumulator(int var1, int* var2){ *var2 += var1; return(var1+*var2); } int main (int argc, char *argv[]) { int var1 = 10, var2 = 20, var3 = 30, result = 0; result=accumulator(var1,&var2); result=accumulator(var2,&var3); printf("%d %d %d %d\n",var1, var2, var3, result); }
Ans: 10 30 60 90
15. What is the output of program given below.
#include<stdio.h> void size_test(int b[]); int main (int argc, char * argv[]) { int a = 5; int b[15] = { 0 } ; size_test(b); printf ("%lu %lu\n",sizeof(a), sizeof(b)); return 0; } void size_test(int b[]) { printf("%lu\n",sizeof(b)); }
Ans:
8
4 60
16. Trace control flow of above example.
#include <stdio.h> int add_array(int arr[], int size); int main (int argc, char* argv[]) { int arr[4] = { 9, 3, 7, 12 }; int sum=0; sum = add_array(arr, 4); printf("Sum of array is %d\n",sum); } int add_array(int * arr, int size) { int result = 0, i = 0; for(i=0; i<size; arr++,i++) { result += *arr; } return result; }
17. Which of the following statements are true.
1. Passing an array to a function is equivalent to passing address of first element of the array.
Ans: True.
2. Incrementing a pointer pointing to an array element makes the pointer point to next array element.
Ans: True.
3. Increment a pointer increases the address stored in pointer by 1.
Ans: False. Address is increased by size of data type pointed by the pointer.
18. Modify add_array function above to use a countdown loop.
int add_array(int * arr, int size) { int result = 0, i = 0; for(i=size-1; i>=0; arr++,i--) { result += *arr; } return result; }
19. Assume sizeof(double) is 8, sizeof(* double) is 4, dptr is a pointer to double with address 80 stored in it. What will be the address in dptr after dptr++ is executed.
Ans: 88. Incrementing a pointer increases the pointer value by size of data type pointed by the pointer.
20. Assume sizeof(double) is 8, sizeof(* double) is 4, darr is a array of double with 100 elements. If darr is passed to a function in which sizeof(darr) is printed, what will be the printed value?
Ans: 4. We will be printing sizeof pointer to double.
21. Write a function that takes temperature readings of last n days as parameter and provides back average temperature of last seven days and last n days. If n<7 then average of last seven days should be given as zero.
void temp_averages(float *arr, int size, float *avg_7, float *avg_n) { int i = 0; float sum = 0; *avg_7 = 0; *avg_n = 0; for(i=0; i<size; arr++,i++) { sum += arr[i]; if(6 == i) { *avg_7 = sum/7; } } *avg_n = sum/size; }
22. Which of the following statements are true.
1. We can typecast pointers of any type to any other type.
Ans: True.
2. Given iptr as pointer to int and dptr as pointer to double and typecast
dptr = (double *)iptr;
Assuming 4 byte int and 8 byte double, incrementing dptr will increase the address in dptr by 8.
Ans: True.
23. Which of the following statements are true.
1. An int ** can only hold address of an int*.
Ans: False. It can hold any address by typecasting.
2. An int ** can only hold address of any integer type.
Ans: False. It can hold any address by typecasting.
3. A pointer to pointer cannot be incremented or decremented.
Ans: False. Imagine iterating through an array of pointers.
24. If a pointer to pointer is incremented then what would location would it point to?
Ans: Next pointer location.
25. What will be the output of below program.
#include <stdio.h> void mixer(int **pptr){ int ** temp = pptr; for(int i=0; i<2; i++) { **temp += **(temp+1); temp++; } } int main (int argc, char *argv[]) { int *ptrarr[3] = {0}; int a=10,b=20,c=30; ptrarr[0]=&a; ptrarr[1]=&b; ptrarr[2]=&c; mixer(ptrarr); for(int i=0; i<3; i++) { printf("%d\n",*ptrarr[i]); } }
Ans:
30
50
30