0% found this document useful (0 votes)
5 views29 pages

Programming in C Unit IV

The document provides an overview of pointers in C programming, detailing their definitions, usage, and various types such as function pointers, void pointers, and pointers to pointers. It includes examples and programs demonstrating pointer arithmetic, dynamic memory allocation, and the relationship between pointers and arrays. Additionally, it covers concepts like memory leaks, null pointers, and the use of typedef, along with practical programming examples for each topic.

Uploaded by

boomikag2006
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)
5 views29 pages

Programming in C Unit IV

The document provides an overview of pointers in C programming, detailing their definitions, usage, and various types such as function pointers, void pointers, and pointers to pointers. It includes examples and programs demonstrating pointer arithmetic, dynamic memory allocation, and the relationship between pointers and arrays. Additionally, it covers concepts like memory leaks, null pointers, and the use of typedef, along with practical programming examples for each topic.

Uploaded by

boomikag2006
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
You are on page 1/ 29

Programming in C

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;

How do you declare and initialize a pointer?


Declare a pointer using * and initialize it with the address of a variable using &.
Example:
int b = 20;
int *ptr = &b;

What is the use of the * and & operators?


* is used to declare or dereference a pointer. & returns the address of a variable.
Example:
int x = 5;
int *px = &x;
int y = *px;

Define pointer arithmetic.


Pointer arithmetic means incrementing or decrementing pointers to access
consecutive elements in arrays.
Example:
int arr[] = {1, 2, 3};
int *p = arr;
p++;
How does a pointer relate to an array?
An array name is a pointer to its first element.
Example:
int arr[3] = {10, 20, 30};
int *ptr = arr;

What is a void pointer?


A void pointer can point to any data type but must be cast before dereferencing.
Example:
int x = 100;
void *vp = &x;

What is a function pointer?


A function pointer stores the address of a function and can be used to call it.
Example:
int add(int a, int b) { return a + b; }
int (*fptr)(int, int) = add;

What is a pointer to pointer?


It stores the address of a pointer variable.
Example:
int a = 5;
int *p = &a;
int **pp = &p;

How do you declare an array of pointers?


An array that holds multiple pointers.
Example:
int a = 10, b = 20;
int *arr[2] = {&a, &b};
What is dynamic memory allocation?
Allocating memory at runtime using functions like malloc and calloc.
Example:
int *ptr = (int *)malloc(5 * sizeof(int));

What is malloc()?
Allocates uninitialized memory and returns a void pointer.
Example:
int *arr = (int *)malloc(10 * sizeof(int));

How is calloc() different from malloc()?


calloc() allocates memory and initializes it to zero.
Example:
int *arr = (int *)calloc(5, sizeof(int));

Why use free()?


To release dynamically allocated memory back to the system.
Example:
free(arr);

What does typedef do?


Creates a new name or alias for a data type.
Example:
typedef unsigned int u32;
u32 num = 100;

What are command line arguments?


Arguments passed to main from the command line at program start.
Example:
int main(int argc, char *argv[])
Can a pointer be incremented?
Yes, to point to the next element in memory based on the pointer’s data type.
Example:
int arr[3] = {1, 2, 3};
int *p = arr;
p++;

What does *(arr + i) represent?


It accesses the ith element of the array.
Example:
int arr[] = {5, 10, 15};
int val = *(arr + 2);

How is a pointer different from an array?


A pointer stores an address and can be reassigned, an array is a fixed block of
memory.
Example:
int arr[3] = {1, 2, 3};
int *p = arr;

What are wild pointers?


Pointers that are uninitialized and point to random memory.
Example:
int *p;

What is memory leak?


When dynamically allocated memory is not freed and lost.
Example:
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
General Questions

Definition and Basics of Pointers in C


A pointer is a variable that stores the address of another variable. It allows
indirect manipulation of data by accessing the memory location. Pointers are
essential for dynamic memory management, arrays, and function arguments.
Example:
int a = 10;
int *p = &a;
Program:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%d\n", *p);
return 0;
}

2. Pointer Initialization and Pointer Operators


Pointer initialization means assigning the address of a variable to a pointer. The
operators used with pointers are & (address of) and * (dereference).
Example:
int a = 5;
int *p = &a;
Program:
#include <stdio.h>
int main() {
int a = 5;
int *p = &a;
printf("Address: %p\n", p);
printf("Value: %d\n", *p);
return 0;
}

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;
}

4. Pointers and One-Dimensional Arrays


Arrays and pointers are closely related in C. The name of an array is treated as a
pointer to the first element. Pointer arithmetic helps in traversing the array.
Example:
int arr[3] = {1, 2, 3};
int *p = arr;
Program:
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int *p = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(p + i));
}
return 0;
}

5. Pointers and Multi-Dimensional Arrays


A pointer to a multi-dimensional array points to an entire row. For a 2D array,
int (*p)[cols] points to rows, allowing access to elements.
Example:
int arr[2][3];
int (*p)[3] = arr;
Program:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = arr;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", p[i][j]);
}
printf("\n");
}
return 0;
}
6. Array of Pointers
An array of pointers is an array where each element is a pointer to a variable or
array. Useful for handling arrays of strings or multiple data references.
Example:
int a = 10, b = 20;
int *arr[2] = {&a, &b};
Program:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *arr[2] = {&a, &b};
for(int i = 0; i < 2; i++) {
printf("%d ", *arr[i]);
}
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;
}

10. Pointer to Function


Pointer to function stores the address of a function and is used to call it
indirectly.
Example:
int add(int a, int b) { return a + b; }
Program:
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main() {
int (*fp)(int, int) = add;
printf("%d\n", fp(3, 4));
return 0;
}

11. Dynamic Memory Allocation


Dynamic memory allocation allows memory to be allocated during runtime
using malloc, calloc, or realloc.
Example:
int *p = malloc(5 * sizeof(int));
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = malloc(5 * sizeof(int));
for(int i = 0; i < 5; i++) p[i] = i + 1;
for(int i = 0; i < 5; i++) printf("%d ", p[i]);
free(p);
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;
}

13. Command Line Arguments


Command line arguments are parameters passed to a program at execution time
via argc and argv.
Example:
int main(int argc, char *argv[])
Program:
#include <stdio.h>
int main(int argc, char *argv[]) {
for(int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}

14. Pointer Typecasting


Pointer typecasting changes the pointer's data type to access different types of
data using the same address.
Example:
void *vp;
int a = 10;
vp = &a;
int *ip = (int*)vp;
Program:
#include <stdio.h>
int main() {
int a = 10;
void *vp = &a;
int *ip = (int*)vp;
printf("%d\n", *ip);
return 0;
}

15. Null Pointer


A null pointer points to nothing. Dereferencing it leads to undefined behavior.
Example:
int *p = NULL;
Program:
#include <stdio.h>
int main() {
int *p = NULL;
if(p == NULL) printf("Pointer is null\n");
return 0;
}

16. Pointer to Constant and Constant Pointer


Pointer to constant points to constant data (data can't be changed), constant
pointer cannot point to another address.
Example:
const int *p1;
int *const p2;
Program:
#include <stdio.h>
int main() {
int a = 10;
const int *p1 = &a;
int *const p2 = &a;
printf("%d %d\n", *p1, *p2);
return 0;
}

17. Dangling Pointer


A pointer pointing to a memory location that has been freed/deallocated is
called dangling pointer.
Example:
int *p = malloc(sizeof(int));
free(p);
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = malloc(sizeof(int));
free(p);
p = NULL;
if(p == NULL) printf("Pointer reset to NULL\n");
return 0;
}

18. Use of sizeof with Pointers


sizeof returns the size of the pointer type or the size of the data it points to if
dereferenced.
Example:
int *p;
printf("%lu\n", sizeof(p));
Program:
#include <stdio.h>
int main() {
int *p;
printf("%lu\n", sizeof(p));
return 0;
}

19. Pointer to Void vs Pointer to Char


Void pointer is generic; char pointer used for string operations.
Example:
void *vp;
char *cp = "Hello";
Program:
#include <stdio.h>
int main() {
char *cp = "Hello";
void *vp = cp;
printf("%s\n", (char*)vp);
return 0;
}

20. Relationship Between Pointers and Arrays


Array name acts as pointer to the first element, pointers can traverse arrays
using arithmetic.
Example:
int arr[] = {1, 2, 3};
int *p = arr;
Program:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3};
int *p = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(p + i));
}
return 0;
}
Eight-mark sized questions

Explain Pointers in C with examples.


Pointers are variables that store the memory address of other variables. They
enable indirect access and manipulation of data stored in memory, allowing
efficient array handling, dynamic memory management, and passing variables
by reference to functions.
Example:
int a = 10;
int *p = &a;
Program:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%d\n", *p);
printf("%p\n", p);
return 0;
}

2. Discuss Pointer Arithmetic with suitable example and program.


Pointer arithmetic allows moving a pointer through consecutive memory
locations by adding or subtracting integers. When a pointer is incremented, it
points to the next element of its data type in memory.
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;
}

3. Explain the relationship between pointers and one-dimensional arrays.


The name of an array is a constant pointer to its first element. Pointers can be
used to traverse and manipulate arrays efficiently through pointer arithmetic.
Example:
int arr[] = {1, 2, 3};
int *p = arr;
Program:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3};
int *p = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(p + i));
}
return 0;
}

4. Describe pointers and multi-dimensional arrays.


A multi-dimensional array is an array of arrays. A pointer to such an array
points to an entire row. Pointer arithmetic can be used to access elements row-
wise and column-wise.
Example:
int arr[2][3];
int (*p)[3] = arr;
Program:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = arr;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", p[i][j]);
}
printf("\n");
}
return 0;
}

5. Explain Array of pointers with examples and program.


An array of pointers stores addresses of variables or arrays. It is commonly used
for arrays of strings or complex data management.
Example:
int a = 10, b = 20;
int *arr[2] = {&a, &b};
Program:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *arr[2] = {&a, &b};
for(int i = 0; i < 2; i++) {
printf("%d ", *arr[i]);
}
return 0;
}

6. Write about Pointer to pointer with example and program.


A pointer to pointer stores the address of another pointer. It allows multiple
levels of indirection useful in complex data structures.
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;
}

7. Explain Pointer to an array with example and program.


Pointer to an array points to the whole array. It holds the address of the entire
array rather than a single element.
Example:
int arr[5];
int (*p)[5] = &arr;
Program:
#include <stdio.h>
int main() {
int arr[5] = {1,2,3,4,5};
int (*p)[5] = &arr;
for(int i = 0; i < 5; i++) {
printf("%d ", (*p)[i]);
}
return 0;
}

8. Write about Void Pointer with example and program.


A void pointer is a generic pointer that can point to any data type. It requires
explicit casting to be dereferenced.
Example:
int a = 10;
void *p = &a;
Program:
#include <stdio.h>
int main() {
int a = 10;
void *p = &a;
printf("%d\n", *((int*)p));
return 0;
}

9. Explain Pointer to function with example and program.


Pointer to function stores the address of a function and is used to call functions
indirectly.
Example:
int add(int, int);
int (*funcPtr)(int, int) = add;
Program:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int) = add;
printf("%d\n", funcPtr(5, 10));
return 0;
}

10. Discuss Dynamic Memory Allocation in C with example program.


Dynamic memory allocation lets you allocate memory during program
execution using malloc, calloc, realloc, and free functions.
Example:
int *ptr = malloc(5 * sizeof(int));
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *ptr = (int*)malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
for(int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}

Explain the concept of pointer to a constant and constant pointer in C with


examples and program.
A pointer to a constant points to data that cannot be changed through the
pointer, but the pointer itself can point elsewhere. A constant pointer cannot
change the address it holds but can modify the data it points to.
Example:
const int a = 10;
const int *p = &a;
int b = 20;
int *const cp = &b;
Program:
#include <stdio.h>
int main() {
const int a = 10;
const int *p = &a;
printf("%d\n", *p);

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;
}

14. Explain how dynamic memory allocation is handled for multi-


dimensional arrays in C with a program.
Dynamic allocation for multi-dimensional arrays uses pointer to pointer and
nested malloc calls to allocate rows and columns at runtime.
Example:
Allocate a 2D array dynamically.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 2, cols = 3;
int **arr = (int**)malloc(rows * sizeof(int*));
for(int i = 0; i < rows; i++) {
arr[i] = (int*)malloc(cols * sizeof(int));
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
arr[i][j] = i + j;
}
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
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;
}

You might also like