Below is a concise roadmap of how pointers work in C—from the basics of what they are, through how
## Summary
Pointers in C are variables that hold memory addresses rather than direct values; they enable indirect
## Complete Pointers Concept
### What Is a Pointer?
A pointer is a variable whose value is the memory address of another variable, enabling indirect acces
### Pointer Declaration and Dereferencing
Declaration:
int *p; // 'p' can hold the address of an int
Address-of Operator (&):
int x = 10;
p = &x; // p now holds the address of x
Dereference Operator (*):
*p = 20; // changes x to 20
### Pointer to Pointer
A pointer can itself be pointed to by another pointer (multi-level indirection).
int x = 42;
int *p = &x; // single pointer
int **pp = &p; // pointer to pointer
**pp = 100; // changes x to 100
### Pointer Arithmetic
Adding or subtracting an integer to/from a pointer moves it by that number of elements, not bytes. For
### Void Pointers
A void* can hold the address of any data type but cannot be dereferenced without casting.
### Dynamic Memory Allocation
malloc(size_t n): Allocates n bytes on the heap and returns a void* to the start; contents are uninitialize
calloc(size_t count, size_t size): Allocates count*size bytes and zero-initializes them.
realloc(void *ptr, size_t newSize): Changes the size of a previously allocated block, possibly moving it.
free(void *ptr): Releases a heap block back to the system.
### Stack vs. Heap Allocation
Stack: Local variables and function parameters. Automatic lifetime (freed when scope exits). Fast alloc
Heap: Dynamically allocated via malloc/free. Larger but slower, manual management required.
### Common Pitfalls
Dangling Pointers: After free, the pointer still holds the old address—dereferencing it is undefined.
Wild Pointers: Uninitialized pointers pointing to arbitrary memory.
Buffer Overflows: Pointer arithmetic that goes out of bounds.
Memory Leaks: Forgetting to free allocated memory.
## Tricky Pointer Questions Asked in Interviews
1. Explain the difference between int *p, int p[], and int (*p)[10].
2. What happens if you do p++ on a char* vs. an int*?
3. How would you implement a two-dimensional array function parameter using pointers?
4. Explain dangling pointers and how to avoid them.
5. Write a function that takes a function pointer as an argument and invokes it.
6. What is pointer aliasing and how does it affect optimizations?
7. How do you cast between different pointer types, and what are the risks?
8. Implement your own calloc using malloc.
9. Describe how the compiler might implement malloc under the hood.
10. How do you detect and prevent memory leaks in large C codebases?
## Practice Questions Answers
1. Square and Cube of a Number via Pointers
#include <stdio.h>
void sq_cube(int n, int *sq, int *cube) {
*sq = n * n; // square
*cube = n * n * n; // cube
}
int main() {
int num = 5, s, c;
sq_cube(num, &s, &c);
printf("Square: %d, Cube: %d
", s, c);
return 0;
}
2. Swap Three Integers Using Pointers
#include <stdio.h>
void swap3(int *a, int *b, int *c) {
int temp = *a;
*a = *b;
*b = *c;
*c = temp;
}
int main() {
int x = 1, y = 2, z = 3;
swap3(&x, &y, &z);
printf("x=%d, y=%d, z=%d
", x, y, z);
return 0;
}
3. Modify a Pointer via Pointer-to-Pointer
#include <stdio.h>
#include <stdlib.h>
void set_to_heap(int **p) {
*p = malloc(sizeof(int));
**p = 99;
}
int main() {
int *ptr = NULL;
set_to_heap(&ptr);
printf("Value: %d
", *ptr);
free(ptr);
return 0;
}
4. Zero-Initialize an Array Using Pointers
#include <stdio.h>
void zero_array(int *arr, int n) {
for (int i = 0; i < n; i++) {
*(arr + i) = 0;
}
}
int main() {
int data[5] = {1,2,3,4,5};
zero_array(data, 5);
for (int i=0; i<5; i++) printf("%d ", data[i]);
return 0;
}
5. Find Max and Min in an Array
#include <stdio.h>
void find_min_max(int *arr, int n, int *min, int *max) {
*min = *max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *min) *min = arr[i];
if (arr[i] > *max) *max = arr[i];
}
}
int main() {
int a[] = {3, 7, 1, 9, 4}, mn, mx;
find_min_max(a, 5, &mn, &mx);
printf("Min=%d, Max=%d
", mn, mx);
return 0;
}
6. Dynamic Allocation with malloc
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *arr = malloc(n * sizeof *arr);
if (!arr) return 1;
for (int i = 0; i < n; i++) {
arr[i] = i * i;
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
7. Modify a 2D Array via Pointer Arithmetic
#include <stdio.h>
void add_one(int (*mat)[3], int rows) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < 3; j++)
*(*(mat + i) + j) += 1;
}
int main() {
int m[2][3] = {{1,2,3},{4,5,6}};
add_one(m, 2);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
printf("%d ", m[i][j]);
return 0;
}
8. In-Place String Reversal with Pointers
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
char *end = s + strlen(s) - 1;
while (s < end) {
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}
int main() {
char str[] = "hello";
reverse(str);
printf("%s
", str);
return 0;
}
9. Count Vowels via Pointer Traversal
#include <stdio.h>
#include <ctype.h>
int count_vowels(const char *s) {
int count = 0;
for (; *s; s++) {
char c = tolower(*s);
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
count++;
}
return count;
}
int main() {
printf("%d
", count_vowels("Embedded Systems"));
return 0;
}
10. Call-by-Reference in a Structure
#include <stdio.h>
typedef struct {
char name[20];
int marks;
} Student;
void update_marks(Student *st, int new_marks) {
st->marks = new_marks;
}
int main() {
Student s = {"Rudhresh", 75};
update_marks(&s, 95);
printf("%s: %d
", s.name, s.marks);
return 0;
}
These explanations and code samples cover the full breadth of pointer usage—from basic syntax to dy