Welcome to
GITAM
Dept of CSE, GIT
Double Pointer (Pointers to Pointers)
A double pointer is a pointer that holds the address of another pointer. In other words, it is a
pointer that "points to" a pointer, which in turn "points to" the actual data value.
Double pointers are particularly useful in scenarios where we need to modify the pointer
itself within a function, or in cases involving multi-dimensional arrays and dynamic
memory allocation.
Syntax: int **ptr; — Here, ptr is a double pointer to an integer type.
Dept of CSE, GIT
2-D Arrays (Two Dimensional Arrays)
A 2D array (two-dimensional array) is an array of arrays. It's commonly used to store
data in rows and columns, like a table or matrix.
Each element in a 2D array can be accessed using two indices: one for the row and one
for the column.
Declaration and Syntax:
data_type array_name[rows][columns];
Example: int matrix[3][4]; // A 3x4 matrix (3 rows and 4 columns) of integers
Dept of CSE, GIT
2-D Arrays (Two Dimensional Arrays)
Various Ways of Initializing 2-D arrays:
Programs: Static Initialization of 2-D Array and Dynamic Initialization of 2-D array
Dept of CSE, GIT
Pointer Arrays
Pointer arrays are arrays of pointers, where each element of the array is itself a pointer
to a data type (e.g., int, char, float, etc.).
Pointers to pointers (double pointers) allow the creation of multi-dimensional dynamic
arrays.
Using pointers to pointers can help create dynamic two-dimensional arrays, where each
row can have a different length.
With pointer arrays, memory can be allocated and freed for each individual row or
section, allowing for more efficient memory usage.
Dept of CSE, GIT
Pointer Arrays
Declaring a pointer to a pointer: int **ptr;
Allocating memory for a 2D array: ptr = (int **)malloc(rows * sizeof(int *));
TASK:
Write a program to read the values into the 2-D array and print them without using the
array subscripts?
Answer: using pointers
Write a program to declare a 2-D array and retrieve the values using a pointer?
Dept of CSE, GIT
Command Line Arguments
Command-line arguments are the values given after the name of the program in the
command-line shell of Operating Systems. Command-line arguments are handled by the
main() function of a C program.
To pass command-line arguments, we typically define main() with two arguments: the
first argument is the number of command-line arguments and the second is a list of
command-line arguments.
Syntax:
int main(int argc, char *argv[])
{
/* ... */
}
Dept of CSE, GIT
Command Line Arguments
argc (Argument Count) is an integer variable that stores the number of command-line
arguments passed by the user including the name of the program. So, if we pass a value
to a program, the value of argc would be 2 (one for argument and one for program
name) The value of argc should be non-negative.
argv (Argument Vector) is an array of character pointers listing all the arguments. If
argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.
argv[0] is the name of the program , after that till argv[argc-1] every element is
command -line argument.
Dept of CSE, GIT
Command Line Arguments
Examples:
Add two numbers by passing the numbers as command line arguments
Compute the area of a circle by passing the radius as command line argument
Dept of CSE, GIT
Pointers to functions
Passing the pointers to the function means the memory location of the variables is
passed to the parameters in the function, and then the operations are performed.
The function definition accepts these addresses using pointers, addresses are stored
using pointers.
#include <stdio.h> #include <stdio.h>
void addOne(int*); void addOne(int**);
int main() int main()
{ {
int* p, i = 10; int* p, i = 10;
p = &i; p = &i;
addOne(p); addOne(&p);
printf("%d", *p); // 11 printf("%d", *p); // 11
return 0; return 0;
} }
void addOne(int* ptr) { void addOne(int **ptr) {
(*ptr)++; // adding 1 to *ptr (**ptr)++; // adding 1 to **ptr
} }
Dept of CSE, GIT
Thank You
Dept of CSE, GIT