0% found this document useful (0 votes)
4 views4 pages

Module06 Pointers Solutions

The document provides a comprehensive overview of pointers in C programming, including their definition, usage, and key concepts such as declaration, initialization, and dereferencing. It also covers dynamic memory allocation functions like malloc, calloc, and free, along with practical examples demonstrating pointer arithmetic and array manipulation using pointers. Additionally, it includes a quick revision cheat sheet summarizing essential pointer concepts and syntax.

Uploaded by

riyubale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Module06 Pointers Solutions

The document provides a comprehensive overview of pointers in C programming, including their definition, usage, and key concepts such as declaration, initialization, and dereferencing. It also covers dynamic memory allocation functions like malloc, calloc, and free, along with practical examples demonstrating pointer arithmetic and array manipulation using pointers. Additionally, it includes a quick revision cheat sheet summarizing essential pointer concepts and syntax.

Uploaded by

riyubale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming – Question Bank Solutions

MODULE 06 – Pointers

■ Theory Questions

Q63. What is a pointer in C? How is it useful? Give pointer declaration with an example.
A pointer is a variable that stores the memory address of another variable instead of storing a direct
value. Pointers allow indirect access to memory.
Why pointers are useful:
1. Dynamic memory allocation (malloc, calloc). 2. Passing variables by reference to functions. 3.
Efficient array and string handling. 4. Building data structures like linked lists, trees. 5. Accessing
hardware/system memory directly.
Declaration syntax: datatype *pointer_name;
#include <stdio.h>
int main() {
int num = 42;
int *ptr; // pointer declaration
ptr = &num; // store address of num in ptr

printf("Value of num = %d\n", num); // 42


printf("Address of num = %p\n", &num); // e.g. 0x7ffd...
printf("Value of ptr = %p\n", ptr); // same address
printf("Value via ptr = %d\n", *ptr); // 42 (dereferencing)

*ptr = 100; // change num's value through pointer


printf("New value of num = %d\n", num); // 100
return 0;
}

Q64. Explain the concept of a pointer / pointer variable in C in detail.


Key concepts of pointers:
CONCEPT DESCRIPTION EXAMPLE
---------------------------------------------------------------------------
Declaration Declare with * before name int *p;
Initialization Assign address using & operator p = &x;
Dereferencing Access value at address using * val = *p;
NULL pointer Points to nothing (safe init) int *p = NULL;
Pointer size Always same size (4 or 8 bytes) sizeof(p) = 8
Pointer to pointer Stores address of another pointer int **pp = &p;

/* Detailed example */
#include <stdio.h>
int main() {
int x = 10;
int *p = &x; // p holds address of x
int **pp = &p; // pp holds address of p

printf("x = %d\n", x); // 10


printf("*p = %d\n", *p); // 10
printf("**pp = %d\n", **pp); // 10
printf("address x = %p\n", &x);
printf("value p = %p\n", p); // same as &x
printf("address p = %p\n", &p);
printf("value pp = %p\n", pp); // same as &p
return 0;
}

Q65. Explain dynamic memory allocation functions (malloc, calloc, free) in C with examples.
Dynamic Memory Allocation (DMA) allows programs to request memory at runtime from the heap,
unlike static allocation which is fixed at compile time. Functions are defined in stdlib.h.
FUNCTION SYNTAX DESCRIPTION
------------------------------------------------------------------------
malloc() void* malloc(size_t size) Allocates 'size' bytes.
Memory is uninitialized.
calloc() void* calloc(n, size_t size) Allocates n*size bytes.
Memory initialized to 0.
realloc() void* realloc(ptr, size_t size) Resizes previously
allocated memory block.
free() void free(void *ptr) Releases allocated memory
back to the system.

#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("How many integers? ");
scanf("%d", &n);

/* malloc - allocate memory (uninitialized) */


int *arr1 = (int*) malloc(n * sizeof(int));
if (arr1 == NULL) { printf("malloc failed!\n"); return 1; }
for (i = 0; i < n; i++) arr1[i] = i + 1;
printf("malloc array: ");
for (i = 0; i < n; i++) printf("%d ", arr1[i]);
printf("\n");

/* calloc - allocate memory (initialized to 0) */


int *arr2 = (int*) calloc(n, sizeof(int));
if (arr2 == NULL) { printf("calloc failed!\n"); return 1; }
printf("calloc array (all zeros): ");
for (i = 0; i < n; i++) printf("%d ", arr2[i]);
printf("\n");

/* free - release both arrays */


free(arr1);
free(arr2);
printf("Memory freed successfully.\n");
return 0;
}
/* Output (n=5):
malloc array: 1 2 3 4 5
calloc array (all zeros): 0 0 0 0 0
Memory freed successfully.
*/

■ Program / Practical Questions

Q66. Write a program to display the contents of an array using pointers.


#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, i;
int *ptr = arr; // ptr points to first element of array

printf("Array elements using pointer:\n");


for (i = 0; i < n; i++) {
printf("arr[%d] = %d (address: %p)\n", i, *(ptr + i), (ptr + i));
}

/* Alternative: using ptr++ */


printf("\nUsing ptr++:\n");
ptr = arr; // reset pointer to start
for (i = 0; i < n; i++) {
printf("%d ", *ptr);
ptr++;
}
printf("\n");
return 0;
}
/*
Output:
arr[0] = 10 (address: 0x...)
arr[1] = 20 (address: 0x...)
...
Using ptr++:
10 20 30 40 50
*/

Q67. Write a C program to demonstrate pointer arithmetic.


Pointer arithmetic means performing arithmetic operations on pointers. When you increment a pointer
by 1, it moves to the next element of its type (e.g. int pointer moves 4 bytes forward).
#include <stdio.h>
int main() {
int arr[] = {100, 200, 300, 400, 500};
int *ptr = arr;

printf("Initial ptr points to: %d (address %p)\n", *ptr, ptr);

/* Increment */
ptr++;
printf("After ptr++ : %d (address %p)\n", *ptr, ptr);

/* Add integer to pointer */


ptr = ptr + 2;
printf("After ptr+2 : %d (address %p)\n", *ptr, ptr);

/* Decrement */
ptr--;
printf("After ptr-- : %d (address %p)\n", *ptr, ptr);

/* Pointer subtraction (distance between two pointers) */


int *start = arr;
int *end = &arr[4];
printf("Distance between end and start: %ld elements\n", end - start);

/* Pointer comparison */
if (ptr > start)
printf("ptr is ahead of start.\n");

return 0;
}
/*
Output:
Initial ptr points to: 100
After ptr++ : 200
After ptr+2 : 400
After ptr-- : 300
Distance between end and start: 4 elements
ptr is ahead of start.
*/

Q68. WAP to multiply 5 numbers using pointers and an array.


#include <stdio.h>
int main() {
int arr[5], i;
int *ptr = arr;
long long product = 1;

printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", ptr + i); // store input via pointer
}

printf("Numbers entered: ");


for (i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
product *= *(ptr + i); // multiply using pointer
}

printf("\nProduct of 5 numbers = %lld\n", product);


return 0;
}
/*
Input : 2 3 4 5 6
Output: Product of 5 numbers = 720
*/

■ Quick Revision – Pointer Cheat Sheet


SYMBOL / CONCEPT MEANING EXAMPLE
-----------------------------------------------------------------------
int *p Declare pointer to int int *p;
&x Address of variable x p = &x;
*p Value at address stored in p val = *p;
p++ Move to next memory location ptr++
*(p+i) Access i-th element via pointer *(arr+2) = arr[2]
NULL Pointer pointing to nothing int *p = NULL;
malloc(n*sizeof(int)) Allocate n integers on heap int *p = malloc(...)
free(p) Release heap memory free(p);
sizeof(pointer) Always 4 (32-bit) or 8 (64-bit) sizeof(p) = 8

CP Question Bank – Module 06 Solutions | Questions Q63–Q68 | Total: 6 Questions

You might also like