10-MARK QUESTIONS
1. Question: What is pointer? Explain declaration, initialization and accessing of a pointer
variable.
Theory:
A pointer is a variable that stores the address of another variable rather than storing a direct value.
Pointers are one of the most powerful features of C because they provide direct access to memory,
which allows efficient manipulation of arrays, strings, structures, and dynamic memory. Pointers also
play a crucial role in function calls, especially when passing large data structures, because passing by
address avoids copying entire values, saving both time and memory.
Declaration:
A pointer is declared using the * operator along with a data type that indicates the type of variable it
points to.
data_type *pointer_name;
Initialization:
A pointer must be initialized with the address of a variable using the address-of operator &. For
example:
int a = 10;
int *ptr = &a;
Here, ptr stores the memory address of a.
Accessing the value:
To access or modify the value stored at the address, the dereference operator * is used.
printf("%d", *ptr); // prints value of a
*ptr = 20; // changes value of a to 20
Advantages of pointers:
1. Efficient memory management and dynamic memory allocation.
2. Allow functions to modify variables directly (pass by reference).
3. Useful in handling arrays, strings, and structures.
4. Enable implementation of advanced data structures like linked lists, stacks, and trees.
Applications:
Dynamic memory allocation using malloc, calloc, realloc, and free.
Accessing arrays and strings efficiently.
Implementing data structures such as linked lists, trees, and graphs.
Pointers are essential for writing high-performance and memory-efficient C programs. They allow
programmers to directly interact with memory, which is why understanding declaration, initialization,
and accessing is fundamental for every C programmer.
Program Example:
#include <stdio.h>
int main() {
int a = 10;
int *ptr;
ptr = &a;
printf("Address of a = %p\n", ptr);
printf("Value of a = %d\n", *ptr);
return 0;
}
2. Question: Explain pointer arithmetic in detail.
Theory:
Pointer arithmetic refers to performing operations on pointer variables to navigate memory locations.
In C, pointer arithmetic is scaled by the size of the data type that the pointer points to. This feature
makes it easy to traverse arrays or blocks of memory.
Valid operations on pointers include:
1. Increment (ptr++) – Moves the pointer to the next element of the data type. For an int *ptr,
ptr++ increases the address by 4 bytes (if int is 4 bytes).
2. Decrement (ptr--) – Moves the pointer to the previous element.
3. Addition (ptr + n) – Moves the pointer n elements forward.
4. Subtraction (ptr - n) – Moves the pointer n elements backward.
5. Difference (ptr1 - ptr2) – Gives the number of elements between two pointers pointing to the
same array.
Pointer arithmetic is particularly important for array traversal, string manipulation, and dynamic
memory operations, because it allows efficient access to elements without using index variables.
Applications:
Traversing arrays and linked lists.
Dynamic memory access.
Implementing low-level operations in operating systems and embedded systems.
Program Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("Original value = %d\n", *ptr);
ptr++;
printf("After ptr++, value = %d\n", *ptr);
ptr = ptr + 2;
printf("After ptr+2, value = %d\n", *ptr);
return 0;
}
3. Question: Explain dynamic memory allocation and explain malloc(), calloc(), realloc(), and
free() functions.
Theory:
Dynamic memory allocation is a mechanism by which programs can request memory at runtime,
rather than at compile time. This is essential when the size of data is not known in advance, which
often occurs in real-time systems or programs handling large datasets.
Key functions:
1. malloc(size) – Allocates size bytes of memory. The memory is uninitialized. Returns a
pointer to the allocated memory.
2. calloc(n, size) – Allocates memory for n elements, each of size bytes, and initializes memory
to zero.
3. realloc(ptr, size) – Changes the size of previously allocated memory. Existing data is
preserved up to the minimum of old and new sizes.
4. free(ptr) – Deallocates the memory previously allocated to avoid memory leaks.
Advantages:
Efficient use of memory.
Flexibility to allocate large arrays or data structures at runtime.
Enables dynamic data structures such as linked lists, stacks, and trees.
Program Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, n = 5;
ptr = (int*) malloc(n * sizeof(int));
for(int i=0;i<n;i++) ptr[i] = i+1;
printf("Values: ");
for(int i=0;i<n;i++) printf("%d ", ptr[i]);
printf("\n");
ptr = (int*) realloc(ptr, 7 * sizeof(int));
for(int i=5;i<7;i++) ptr[i] = i+1;
printf("After realloc: ");
for(int i=0;i<7;i++) printf("%d ", ptr[i]);
free(ptr);
return 0;
}
4. Question: Difference between structures and unions with respect to memory allocation.
Explain structures and unions with syntax and examples.
Theory:
Structures and unions allow grouping of different data types under a single name. However, they
differ in memory allocation and access.
Structure:
Each member has separate memory.
All members can be accessed simultaneously.
Size = sum of all member sizes.
Union:
All members share the same memory location.
Only one member can be used at a time.
Size = largest member.
Advantages of structures: Can store multiple fields at once, useful for complex records like
employee details.
Advantages of unions: Memory-efficient for storing variables that are used one at a time.
Program Example (Structure):
#include <stdio.h>
struct Employee {
int id;
char name[20];
float salary;
};
int main() {
struct Employee e1 = {101, "John", 25000};
printf("ID=%d, Name=%s, Salary=%.2f\n", [Link], [Link], [Link]);
return 0;
}
Program Example (Union):
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data d;
d.i = 10;
printf("i = %d\n", d.i);
d.f = 3.14;
printf("f = %.2f\n", d.f); // i overwritten
return 0;
}
5. Question: Develop a C program using pointers to compute the sum and average of all
elements stored in an array.
Theory:
Pointers can efficiently traverse arrays. Using pointers, we can access array elements without using
indices. This is especially useful for dynamic arrays, memory efficiency, and in function calls where
arrays are passed by reference.
Steps:
1. Declare array and pointer.
2. Initialize pointer with array address.
3. Traverse array using pointer arithmetic.
4. Calculate sum and average.
Program Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr, sum = 0;
float avg;
for(int i=0; i<5; i++) sum += *(ptr + i);
avg = sum / 5.0;
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);
return 0;
}
2-MARK QUESTIONS
1. Question: What is a pointer variable? How is it different from an ordinary variable?
Answer:
A pointer variable stores the address of another variable instead of storing a direct value. An
ordinary variable stores the actual value.
Example:
int a = 10; // ordinary variable
int *ptr = &a; // pointer variable storing address of a
printf("Value of a = %d\n", a);
printf("Address of a = %p\n", ptr);
printf("Value through pointer = %d\n", *ptr);
Difference:
Pointer: stores address, allows indirect access.
Ordinary variable: stores actual value, direct access only.
2. Question: What is a null pointer? What is its use?
Answer:
A null pointer is a pointer that does not point to any valid memory location and is typically
assigned NULL. It is used to initialize pointers safely and prevent undefined behavior.
Example:
int *ptr = NULL;
if(ptr == NULL) {
printf("Pointer is null and safe to use\n");
}
Use: To check if a pointer has been assigned before dereferencing, preventing segmentation faults.
3. Question: What is a self-referential structure?
Answer:
A self-referential structure is a structure that contains a pointer to the same type of structure. It is
widely used in linked lists, trees, and other dynamic data structures.
Example:
struct Node {
int data;
struct Node *next; // pointer to same structure
};
Application: Enables building dynamic data structures where elements point to the next element.
4. Question: What is malloc(), calloc(), realloc() and free()? Write syntax.
Answer:
Dynamic memory allocation functions in C allow memory allocation at runtime.
Functions and Syntax:
1. malloc(size) – Allocates size bytes (uninitialized)
ptr = (int*) malloc(n * sizeof(int));
2. calloc(n, size) – Allocates memory for n elements, initialized to 0
ptr = (int*) calloc(n, sizeof(int));
3. realloc(ptr, size) – Resizes previously allocated memory
ptr = (int*) realloc(ptr, new_size);
4. free(ptr) – Frees allocated memory
free(ptr);
Example:
#include <stdio.h>
#include <stdlib.h>
int *ptr = (int*) malloc(5 * sizeof(int));
free(ptr);
5. Question: Explain the difference between arrays and structures.
Answer:
Feature Array Structure
Elements All elements same type Members can be different types
Access By index By member name
Memory Contiguous Depends on member types
Example int arr[5]; struct Employee { int id; char name[20]; };
Explanation:
Arrays are used to store multiple values of the same type, whereas structures are used to group
different types of data under a single name, like storing an employee’s id, name, and salary together.