Chapter 1: Pointers
1 Mark Questions
Define Pointer.
→ Pointer stores the address of another variable.
What is the size of pointer in 32-bit system?
→ 4 Bytes.
Name any two dynamic memory allocation functions.
→ malloc(), calloc()
What is a dangling pointer?
→ A pointer pointing to a memory location which is freed.
Give syntax of pointer declaration.
→ data_type *pointer_name;
2 Marks Questions
What is pointer arithmetic?
→ Performing arithmetic operations like addition, subtraction, increment, decrement
on pointers.
Differentiate between malloc() and calloc().
→ malloc() allocates single block, calloc() allocates multiple blocks and
initializes with 0.
Explain * (dereference) operator.
→ Used to access value at the address stored by pointer.
Write syntax to declare pointer to array.
→ int *ptr; ptr = arr;
What is pointer to pointer?
→ Pointer that stores address of another pointer.
3 Marks Questions
Explain the relationship between array and pointer.
→ Array name is a constant pointer to its first element.
What is memory leak?
→ Memory allocated dynamically but not freed leads to memory wastage.
Write a program to swap two numbers using pointers.
→ (Simple program using call by reference)
Explain types of pointers.
→ Null, Wild, Dangling, Void, Function pointer.
Explain malloc(), calloc(), realloc(), free().
→ Functions used for dynamic memory management.
4 Marks Questions
Q1) Write a program to store and display 5 elements using pointer.
#include <stdio.h>
int main() {
int arr[5], i;
int *ptr;
ptr = arr;
printf("Enter 5 elements:\n");
for(i = 0; i < 5; i++) {
scanf("%d", ptr + i);
}
printf("Elements are:\n");
for(i = 0; i < 5; i++) {
printf("%d\n", *(ptr + i));
}
return 0;
}
Q2) Explain function pointer with example.
#include <stdio.h>
void display() {
printf("Function called using function pointer\n");
}
int main() {
void (*fptr)(); // Declaration of function pointer
fptr = display; // Assigning address of function
fptr(); // Calling function using pointer
return 0;
}
Q3) Write a program to dynamically allocate memory using malloc().
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL) {
printf("Memory not allocated.\n");
return 0;
}
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("Elements are:\n");
for(i = 0; i < n; i++) {
printf("%d\n", *(ptr + i));
}
free(ptr);
return 0;
}
Q4) Differentiate between static memory allocation and dynamic memory allocation.
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated at compile time Memory is allocated at run time
Size is fixed and cannot be changed Size can be changed (resize
possible)
Uses variables or arrays Uses malloc(), calloc(), realloc()
Memory is automatically managed User needs to manually free
memory
Q5) Explain pointer with function - call by reference with example.
Explanation:
In call by reference, address of actual variables are passed to function using
pointers.
Changes made in function affect the original values.
#include <stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before Swap: x=%d, y=%d\n", x, y);
swap(&x, &y); // Passing address of x and y
printf("After Swap: x=%d, y=%d\n", x, y);
return 0;
}