UNIT 4
UNIT 4
return 0;
}
Pointers to Pointers
Basic Pointer: A pointer holds the address of a variable.
int *p; // p is a pointer to an integer.
Pointer to Pointer: A pointer to a pointer holds the address of
another pointer.
int **q; // q is a pointer to a pointer to an integer.
Dereferencing:
1. Dereferencing p once (*p) gives the value stored in the
variable a.
2. Dereferencing q once (*q) gives the value stored in p (which
is the address of a).
3. Dereferencing q twice (**q) gives the value stored in a.
Example
#include <stdio.h>
Output
int main() { *p = 5
int a = 5; // Declare an integer variable **q = 5
int *p; // Pointer to an integer
int **q; // Pointer to a pointer to an integer
return 0;
}
Array of Pointers:
Definition:
An array of pointers is declared as:
data_type *array_name[size];
•data_type indicates the type of data the pointers will point to.
•Each element of the array is a pointer.
Memory Representation:
Each element in the array stores the address of some data.
Example: Array of Pointers to Integers
#include <stdio.h>
OUTPUT
Value of a: 10
int main() {
Value of b: 20
int a = 10, b = 20, c = 30;
Value of c: 30
// Array of pointers to integers
int *arr[3];
return 0;
}
Example: Array of Pointers to Strings
return 0;
}
POINTER TO AN ARRAY
A pointer to an array stores the address of the first element of the array, but it also
retains information about the size of the array it points to.
Syntax of Pointer to an Array:
data_type (*pointer_name)[size];
data_type: The type of elements stored in the array.
pointer_name: The name of the pointer.
size: The size of the array it points to.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
// Iterating through the array using the pointer •(*ptr) gives access to the array itself.
for (int i = 0; i < 5; i++) { •(*ptr)[i] accesses the i-th element of
printf("Element %d: %d\n", i, (*ptr)[i]); the array.
}
return 0;
}
Dynamic Memory Allocation Functions:
Dynamic memory allocation allows the program to request and manage memory
during runtime (as opposed to compile-time).
It provides flexibility, as the size of data structures like arrays can be decided during
execution, rather than being hard-coded.
C provides several functions for dynamic memory allocation in the <stdlib.h> library.
Function Description
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Assigning values to the allocated memory OUTPUT
for (int i = 0; i < 5; i++) { 12345
ptr[i] = i + 1;
}
return 0;
}
2. calloc (Contiguous
Allocation)
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
return 0;
}
3. realloc (Reallocation)
•Changes the size of an already allocated memory block.
•Can expand or shrink the memory block.
•If the block is expanded, the new part is uninitialized.
Syntax: void *realloc(void *ptr, size_t new_size);
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Assigning initial values
for (int i = 0; i < 3; i++) { Output:
ptr[i] = i + 1;
} 12345
if (ptr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
return 0;
}
4. free (Memory Deallocation)
•Releases previously allocated memory.
•Prevents memory leaks by making memory available for future use.
Syntax: void free(void *ptr);
Example:
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
// After usage
free(ptr); // Deallocate memory
return 0;
}
Comparison
No
No Initializes to
Initialization initialization
initialization zero
for new part
// Step 1: Allocate memory using malloc // Add new values to the extended array
int *arr = (int *)malloc(n * sizeof(int)); for (int i = 5; i < n; i++) {
if (arr == NULL) { arr[i] = i + 1;
printf("Memory allocation failed\n"); }
return 1;
} // Print all values
for (int i = 0; i < n; i++) {
// Step 2: Initialize values printf("%d ", arr[i]);
for (int i = 0; i < n; i++) { }
arr[i] = i + 1;
} // Step 4: Free memory
free(arr);
return 0; Output:
} 1 2 3 4 5 6 7 8 9 10
Arguments to main()
It is mainly useful for command-line programs that need to handle user input or file
paths. The arguments to main() are:
int main(int argc, char *argv[])
a. argc (Argument Count)
Stands for argument count.
It represents the number of arguments passed to the program, including the program
name itself.
It is always at least 1 because the first argument is the program's name.
b. argv (Argument Vector)
Stands for argument vector.
It is an array of strings (char *argv[]) that contains the command-line arguments
passed to the program.
argv[0] is the program's name or path.
argv[1] to argv[argc - 1] are the additional arguments provided by the user.
Alternative Declaration
Some compilers support char **argv instead of char *argv[]. Both are equivalent.
Example
return 0;
}