February 20th, 2022
[Scroll to the bottom of this page to discuss stuff].
First complete last class’ tasks.
Do the following tasks, for more questions you can refer to the book “C in Depth”.
Input two numbers and find their sum using their pointers.
As we talked that arrays are a pointer to the first element of the array, write a program to find the sum of the elements of an array using pointers.
WAP (write a program) to verify that the following ways of accessing 3rd element of an array are the same:
WAP to take an array in input and pass it to a function which reverses the array (without the use of any other array). Then print the reversed array in the main
function.
Since array is passed by reference, the changes made in the function will be reflected in the original array.
WAP to take a number as input and print the cube of that number. This program must use a function with void
return type which must take a single parameter of type int *
. The value at the passed address must be changed.
What is the output of the following code?
#include <stdio.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
void main() {
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", f(c,b,a));
}
Find the output of the following code snippet:
#include<stdio.h>
int main() {
int arr[4]={10,20,30,40}; // Assume base address of arr is 5000
int x=100,*ptr=arr;
printf("%u %d %d\n", ptr, *ptr, x);
x=*ptr++;
printf("%u %d %d\n", ptr, *ptr, x);
x=*++ptr;
printf("%u %d %d\n", ptr, *ptr, x);
x=++*ptr;
printf("%u %d %d\n" ,ptr, *ptr, x);
x=(*ptr)++;
printf("%u %d %d\n", ptr, *ptr, x);
}
What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.
#include <stdio.h>
int main()
{
unsigned int x[4][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
}
Read about dynamic memory allocation and file handling in C.
This is a reminder to keep practicing questions from CodeForces/ CodeChef/ HackerRank/ HackerEarth.