Unit-4 LDP
Unit-4 LDP
3. Pointer Arithmetic:
Pointers can be incremented or decremented, which moves the pointer to the next or previous
memory location of its type. For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
ptr++; // ptr now points to the second element of arr
4. NULL Pointer:
Pointers can be assigned a special value called NULL which means they are not pointing to
any memory location. It's a good practice to initialize pointers to NULL before assigning
them a valid memory address, to avoid accidental access to undefined memory locations.
6. Function Pointers:
Pointers can also point to functions, allowing you to create arrays of functions, pass functions
as arguments to other functions, etc. This is an advanced topic but very powerful in certain
situations.
Pointer to Pointer
A pointer to a pointer in C, also known as a double pointer, is a pointer variable that can hold
the address of another pointer variable. This concept is particularly useful when working with
multi-dimensional arrays or when dealing with dynamic memory allocation.
Function Arguments: Pointer to pointer can be used when you want to modify the original
pointer inside a function
void modifyPointer(int **ptr) {
*ptr = (int *)malloc(sizeof(int));
**ptr = 42;
}
// Usage
int *ptr;
modifyPointer(&ptr);
Multi-dimensional Arrays: Pointer to pointer is useful for dynamically allocating memory
for a 2D array.
cCopy code
int rows = 3, columns = 4; int **matrix = (int **)malloc(rows * sizeof(int *)); for(int i = 0; i
< rows; i++) { matrix[i] = (int *)malloc(columns * sizeof(int)); }
Remember, when using pointer to pointer, you need to be careful with memory management
to avoid memory leaks. Always free the memory allocated dynamically when it is no longer
needed using the free() function.
pointer to array
A pointer to an array can be used to access the elements of an array. When you declare an
array, the name of the array represents the memory address of the first element of the array.
Therefore, a pointer to an array is essentially a pointer to the first element of the array. Here's
how you can use pointers to access and manipulate arrays in C:
1. Pointer to the First Element of an Array:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
In this example, ptr is a pointer that points to the first element of the arr array.
2. Accessing Array Elements using Pointers:
You can use pointer arithmetic to access array elements:
int firstElement = *ptr; // Accesses the first element of the array (same as arr[0])
int thirdElement = *(ptr + 2); // Accesses the third element of the array (same as arr[2])
3. Looping Through an Array using Pointers:
You can use a pointer to iterate through an array:
int *ptr = arr;
for (int i = 0; i < 5; ++i) {
printf("%d ", *(ptr + i));
}
// Output: 1 2 3 4 5
4. Pointer Arithmetic with Arrays:
Pointers and arrays in C are closely related. When you perform pointer arithmetic, the
compiler automatically scales the offset based on the size of the data type.
For example:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);
// Output: 1 2 3 4 5
return 0;
}
In the printArray function, arr is a pointer to the first element of the array passed from the
main function.
array to pointer
An array can decay into a pointer to its first element in certain contexts. This means that
when you use the name of an array in an expression, it is automatically converted to a pointer
to the first element of the array. Here's how it works:
1. Array to Pointer Conversion:
When you declare an array:
int arr[5] = {1, 2, 3, 4, 5};
The name arr represents the memory address of the first element of the array. So, arr can be
used wherever a pointer to an integer is expected.
2. Using Array Name as a Pointer:
You can use the array name as a pointer to its first element:
int *ptr = arr; // ptr points to the first element of arr
In this case, ptr is a pointer that points to the first element of the arr array.
3. Passing an Array to a Function:
When you pass an array to a function, you're actually passing a pointer to the first element of
the array. For example:
void printArray(int *arr, int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);
// Output: 1 2 3 4 5
return 0;
}
In the printArray function, arr is treated as a pointer to the first element of the array passed
from the main function.
4. Array Name in Expressions:
When used in most expressions, the array name decays into a pointer to its first element:
int x = arr[2]; // Accesses the third element of the array using array subscript notation
int y = *(arr + 2); // Accesses the third element of the array using pointer arithmetic
Both arr[2] and *(arr + 2) access the third element of the array arr.
5. Array as Function Argument:
Arrays are commonly passed to functions as pointers:
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; ++i) {
arr[i] *= 2;
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
modifyArray(arr, 5);
// arr now contains: 2 4 6 8 10
return 0;
}
In the modifyArray function, arr is treated as a pointer to its first element. Any changes made
to arr inside the function are reflected in the original array.
Remember that this array-to-pointer decay applies in most contexts, but not all. For instance,
sizeof(arr) still gives you the size of the whole array, not the size of a pointer.
int* createInt() {
int *ptr = (int *)malloc(sizeof(int));
*ptr = 42;
return ptr;
}
int main() {
int *numPtr = createInt();
printf("Value: %d\n", *numPtr); // Output: Value: 42
free(numPtr); // Don't forget to free the allocated memory
return 0;
}
In this example, the createInt function allocates memory for an integer, assigns a value of 42
to it, and returns a pointer to the allocated memory. The main function calls createInt and
prints the value.
2. Function Returning Pointer to Array:
#include <stdio.h>
int main() {
int size = 5;
int *arrPtr = createArray(size);
for (int i = 0; i < size; ++i) {
printf("%d ", arrPtr[i]); // Output: 0 2 4 6 8
}
free(arrPtr); // Don't forget to free the allocated memory
return 0;
}
In this example, the createArray function dynamically allocates memory for an array of
integers, initializes it, and returns a pointer to the first element of the array.
3. Function Returning Pointer to String:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* createString(const char* input) {
char *str = (char *)malloc(strlen(input) + 1);
strcpy(str, input);
return str;
}
int main() {
const char *text = "Hello, World!";
char *strPtr = createString(text);
printf("String: %s\n", strPtr); // Output: String: Hello, World!
free(strPtr); // Don't forget to free the allocated memory
return 0;
}
In this example, the createString function dynamically allocates memory for a string, copies
the input string into the allocated memory, and returns a pointer to the first character of the
new string.
When using functions that return pointers, it's crucial to manage memory properly.
Remember to deallocate the memory using the free function once you are done using the data
to prevent memory leaks.