Programming in C Unit IV
Programming in C Unit IV
Unit IV : Pointers
Two-Mark sized Questions (Note : Just for Reference, not a question-paper answers)
What is a pointer in C?
A pointer is a variable that stores the address of another variable. It allows direct
access to the memory location of data.
Example:
int a = 10;
int *p = &a;
What is malloc()?
Allocates uninitialized memory and returns a void pointer.
Example:
int *arr = (int *)malloc(10 * sizeof(int));
3. Pointer Arithmetic
Pointer arithmetic moves a pointer to the next or previous element by adding or
subtracting integers. It helps access array elements by changing the pointer's
address.
Example:
int arr[] = {10, 20, 30};
int *p = arr;
p++;
Program:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d\n", *p);
p++;
printf("%d\n", *p);
return 0;
}
7. Pointer to Pointer
Pointer to pointer is a variable that stores the address of another pointer. Useful
in multi-level indirection.
Example:
int a = 5;
int *p = &a;
int **pp = &p;
Program:
#include <stdio.h>
int main() {
int a = 5;
int *p = &a;
int **pp = &p;
printf("%d\n", **pp);
return 0;
}
8. Pointer to an Array
Pointer to an array points to the whole array, not just the first element. Syntax:
int (*p)[size].
Example:
int arr[3] = {1, 2, 3};
int (*p)[3] = &arr;
Program:
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int (*p)[3] = &arr;
for(int i = 0; i < 3; i++) {
printf("%d ", (*p)[i]);
}
return 0;
}
9. Void Pointer
Void pointer is a generic pointer that can point to any data type. It must be
typecast before dereferencing.
Example:
int a = 10;
void *vp = &a;
Program:
#include <stdio.h>
int main() {
int a = 10;
void *vp = &a;
printf("%d\n", *(int*)vp);
return 0;
}
12. Typedef in C
typedef is used to create a new name for an existing type. It simplifies complex
type declarations.
Example:
typedef unsigned int u32;
Program:
#include <stdio.h>
typedef unsigned int u32;
int main() {
u32 num = 100;
printf("%u\n", num);
return 0;
}
int b = 20;
int *const cp = &b;
*cp = 30;
printf("%d\n", *cp);
return 0;
}
12. Explain double pointers in detail with example and a program
demonstrating their use.
Double pointers are pointers that point to other pointers, enabling multi-level
referencing useful in dynamic memory and data structure management.
Example:
int a = 5;
int *p = &a;
int **pp = &p;
Program:
#include <stdio.h>
int main() {
int a = 5;
int *p = &a;
int **pp = &p;
printf("%d\n", **pp);
return 0;
}
13. Discuss how pointers are used to pass arrays and functions to other
functions with examples.
Pointers efficiently pass arrays by reference to functions. Function pointers
enable passing functions as arguments, allowing callback functionality.
Example:
void printArray(int *arr, int size);
void greet(void (*func)());
Program:
#include <stdio.h>
void printArray(int *arr, int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
void greet() {
printf("Hello\n");
}
void caller(void (*func)()) {
func();
}
int main() {
int arr[] = {1, 2, 3};
printArray(arr, 3);
caller(greet);
return 0;
}
15. Explain the use of typedef with pointers and provide a sample program
using typedef for pointers.
typedef can create new names for pointer types to simplify complex
declarations and improve code readability.
Example:
typedef int* IntPtr;
Program:
#include <stdio.h>
typedef int* IntPtr;
int main() {
int a = 5;
IntPtr p = &a;
printf("%d\n", *p);
return 0;
}
16. Describe the void pointer’s role in implementing generic data structures
with an example.
Void pointers store addresses of any data type, enabling implementation of
generic data structures like linked lists or stacks.
Example:
Generic node with void pointer data.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
void *data;
struct Node *next;
};
int main() {
int a = 5;
struct Node n;
n.data = &a;
n.next = NULL;
printf("%d\n", *((int*)n.data));
return 0;
}
17. Explain command line arguments and demonstrate their use with a
program.
Command line arguments allow passing inputs to a program at execution. argc
counts arguments, argv stores them as strings.
Example:
Program prints all command line arguments.
Program:
#include <stdio.h>
int main(int argc, char *argv[]) {
for(int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
18. Write a program to implement a function pointer array and explain its
usage.
A function pointer array stores addresses of functions, allowing dynamic
function calls by indexing.
Example:
Array of pointers to arithmetic functions.
Program:
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int main() {
int (*funcArr[3])(int, int) = {add, sub, mul};
for(int i = 0; i < 3; i++) {
printf("%d\n", funcArr[i](10, 5));
}
return 0;
}
19. Write a program to swap two numbers using pointers and explain why
pointers are useful here.
Pointers enable swapping by referencing variables directly without copying
values, saving memory and time.
Program:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
20. Explain memory leaks caused by dynamic memory allocation and how
to avoid them with an example.
Memory leaks occur when allocated memory is not freed, causing wasted
resources. Using free() properly avoids leaks.
Program:
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int) * 5);
// Use memory
free(p);
return 0;
}