1. How is a pointer variable different from a normal?
Ans. A pointer variable (or pointer in short) is basically the same as the other
variables, which can store a piece of data. Unlike normal variable which stores
a value (such as an int, a double, a char), a pointer stores a memory address.
Pointers must be declared before they can be used, just like a normal variable.
2. Why is dynamic memory allocation an efficient memory
management technique?
Ans. Dynamic memory allocation is memory reusability and memory can be
freed when not required. It is more efficient. In this memory allocation
scheme, execution is slower than static memory allocation. Here memory can
be released at any time during the program. So, dynamic memory allocation is
an efficient memory management technique.
3. How many bytes are needed to store an int pointer variable? Is it
the same for a char pointer variable?
Ans. To store an int pointer variable, 4 bytes is required.
Yes, char pointer variable also needs 4 bytes.
4. Write the output of the following code segment.
(a) int *ptr, x = 9; ptr = &x; printf("\n %d",
(*ptr)++);
Output:9
(b)
int *ptr, x = 9;
ptr = &x;
printf("\n %d", (*ptr)++);
printf("\n %d", *ptr);
Output:
9
10
(c) int *ptr, x = 9;
ptr = &x;
int y = ++(*ptr);
printf("\n %d", y);
Output:
10
(d)
char *ptr, x = 'A';
ptr = &x;
char y = *ptr;
printf("\n %c", y);
Output:A
(e)
char *ptr, x = 'A';
ptr = &x;
char y = (*ptr)++;
printf("\n %c", y);
Output:A
(f)
char *ptr, x = 'A';
ptr = &x;
char y = ++(*ptr);
printf("\n %c", y);
Output:B
(g)
char *ptr, x = 'A';
ptr = &x;
char *y;
y = ptr;
printf( "\n %c", ++(*y) );
Output: B
5. Write a C program to dynamically allocate memory for an array to
store 10 integers and display the first 5 out of them.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr; // Declare a pointer to int
int size = 10; // Total size of the array
int i;
// Dynamically allocate memory for the array
arr = (int *)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 0;
}
// Input values into the array
for (i = 0; i < size; i++) {
arr[i] = i + 1; // Filling the array with some values (1 to 10)
}
// Display the first 5 elements of the array
printf("The first 5 elements of the array are: ");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free the dynamically allocated memory
free(arr);
return 0;
}
Output:
The first 5 elements of the array are: 1 2 3 4 5
6. Write a C program to dynamically allocate memory for an array to
store runs scored by Virat Kohli in the last ten ODI cricket matches.
Write a function to find the maximum one.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to find the maximum run scored
int findMaxRun(int *runs, int size) {
int max = runs[0]; // Initialize max with the first run
for (int i = 1; i < size; i++) {
if (runs[i] > max) {
max = runs[i]; // Update max if a higher run is found
return max;
}
int main() {
int *runs; // Declare a pointer to int
int size = 10; // Total number of matches
int maxRun;
// Dynamically allocate memory for the array
runs = (int *)malloc(size * sizeof(int));
if (runs == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
// Input runs scored in the last 10 matches (for demonstration
purposes)
printf("Enter runs scored in the last 10 matches:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &runs[i]);
// Find the maximum run scored using the function
maxRun = findMaxRun(runs, size);
// Display the maximum run scored
printf("Maximum run scored by Virat Kohli in the last 10
matches: %d\n", maxRun);
// Free the dynamically allocated memory
free(runs);
return 0;
}
Output:
Enter runs scored in the last 10 matches:
50
68
102
45
78
93
55
120
35
84
Maximum run scored by Virat Kohli in the last 10 matches: 120
7. Write a C program and define a function that takes the length of
your name as an input parameter and then allocates memory
dynamically to store your name. Write another function to display
the name.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to allocate memory for the name and return a pointer to
it
char *allocateName(int length) {
char *name = (char *)malloc((length + 1) * sizeof(char)); // +1 for
the null terminator
if (name == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(1);
return name;
// Function to display the name
void displayName(char *name) {
printf("Your name is: %s\n", name);
int main() {
int nameLength;
char *yourName;
// Input the length of your name
printf("Enter the length of your name: ");
scanf("%d", &nameLength);
// Allocate memory for your name
yourName = allocateName(nameLength);
// Input your name
printf("Enter your name: ");
scanf("%s", yourName);
// Display your name
displayName(yourName);
// Free the dynamically allocated memory
free(yourName);
return 0;
}
Output:
Enter the length of your name: 10
Enter your name: JohnDoe
Your name is: JohnDoe
8. Write a C program to store some integer variables in an array.
Then write functions to the following:
(a) To calculate the number of even numbers in the array.
(b) To dynamically allocate memory to a new array to a new array to
store only the even numbers.
(c) To copy the even numbers from the first array to the second
one.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the number of even numbers in the array
int countEvenNumbers(int *arr, int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
count++;
}
return count;
// Function to create a new array and copy even numbers from the
original array
int *copyEvenNumbers(int *arr, int size, int evenCount) {
int *evenArr = (int *)malloc(evenCount * sizeof(int));
if (evenArr == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(1);
int j = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
evenArr[j] = arr[i];
j++;
return evenArr;
int main() {
int originalArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(originalArray) / sizeof(originalArray[0]);
// (a) Calculate the number of even numbers in the array
int evenCount = countEvenNumbers(originalArray, size);
printf("Number of even numbers in the array: %d\n", evenCount);
// (b) Dynamically allocate memory for a new array to store even
numbers
int *evenArray = copyEvenNumbers(originalArray, size,
evenCount);
// (c) Display the even numbers from the new array
printf("Even numbers in the new array: ");
for (int i = 0; i < evenCount; i++) {
printf("%d ", evenArray[i]);
printf("\n");
// Free the dynamically allocated memory
free(evenArray);
return 0;
}
Output:
Number of even numbers in the array: 5
Even numbers in the new array: 2 4 6 8 10
9. Write a C program to store some integer variables in an array.
Then write functions to do the following:
(a) To calculate the number of non-zero elements that are divisible
by 3.
(b) To dynamically allocate to store only those elements.
(c) To copy the selected elements from the first array to the second
one.
(d) To calculate the summation of these elements.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the number of non-zero elements divisible
by 3
int countDivisibleBy3(int *arr, int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] != 0 && arr[i] % 3 == 0) {
count++;
return count;
// Function to create a new array and copy elements divisible by 3
int *copyDivisibleBy3(int *arr, int size, int count) {
int *newArr = (int *)malloc(count * sizeof(int));
if (newArr == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(1);
int j = 0;
for (int i = 0; i < size; i++) {
if (arr[i] != 0 && arr[i] % 3 == 0) {
newArr[j] = arr[i];
j++;
return newArr;
// Function to calculate the summation of elements in an array
int calculateSum(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
return sum;
int main() {
int originalArray[] = {1, 0, 9, 12, 6, 15, 0, 18, 3, 0};
int size = sizeof(originalArray) / sizeof(originalArray[0]);
// (a) Calculate the number of non-zero elements divisible by 3
int countDivisible = countDivisibleBy3(originalArray, size);
printf("Number of non-zero elements divisible by 3: %d\n",
countDivisible);
// (b) Dynamically allocate memory for a new array to store
selected elements
int *newArray = copyDivisibleBy3(originalArray, size,
countDivisible);
// (c) Display the selected elements from the new array
printf("Elements divisible by 3 in the new array: ");
for (int i = 0; i < countDivisible; i++) {
printf("%d ", newArray[i]);
printf("\n");
// (d) Calculate the summation of the selected elements
int sum = calculateSum(newArray, countDivisible);
printf("Summation of elements divisible by 3: %d\n", sum);
// Free the dynamically allocated memory
free(newArray);
return 0;
}
Output:
Number of non-zero elements divisible by 3: 5
Elements divisible by 3 in the new array: 9 12 6 15 18
Summation of elements divisible by 3: 60