C dereference pointer
As we already know that "what is a pointer", a pointer is a variable that stores the address of another variable. The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer. When we dereference a pointer, then the value of the variable pointed by this pointer will be returned.
Why we use dereferencing pointer?
Dereference a pointer is used because of the following reasons:It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer.
Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.
Let's observe the following steps to dereference a pointer.
Step 1: Declare a Pointer
Before you can dereference a pointer, you first need to declare a pointer variable. You declare a pointer by specifying its data type followed by an asterisk (*). For example, to declare a pointer to an integer, you would do the following:
cint *ptr; // Declare a pointer to an integer
Step 2: Assign a Memory Address
Once you've declared a pointer, you need to assign it a memory address. This memory address can point to a variable, an array, dynamically allocated memory, or even a function. For example, to make the pointer point to an integer variable num, you do:
cint num = 42;
int *ptr = # // Assign the memory address of 'num' to 'ptr'
Here, &num is the address-of operator, which returns the memory address of the variable num.
Step 3: Dereference the Pointer
Dereferencing a pointer means accessing the value stored at the memory location pointed to by the pointer. This is done using the asterisk (*) operator. To access and retrieve the value stored at the memory location pointed to by ptr, you use the dereference operator like this:
cint value = *ptr; // Dereference 'ptr' and store the value in 'value'
In this case, *ptr accesses and retrieves the value stored at the memory location pointed to by ptr (in this case, the value of num) and assigns it to the variable value.
Let's combine all the above steps:
c#include <stdio.h>
#include <stdlib.h>
// Define a structure for a singly linked list node
struct Node {
int data;
struct Node *next;
};
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to add two integers
int add(int a, int b) {
return a + b;
}
int main() {
// Step 1: Declare and assign a pointer
int num = 42;
int *ptr = # // 'ptr' points to the memory location where 'num' is stored
// Step 2: Dereference the pointer
int value = *ptr; // Dereference 'ptr' to access the value
// Step 3: Work with the value
printf("Step 1: Value stored at the memory location pointed to by 'ptr' is: %d\n", value);
// Arrays and Pointer Arithmetic
int numbers[] = {10, 20, 30, 40, 50};
int *arrayPtr = numbers; // 'arrayPtr' points to the first element of the array
printf("Step 2: First element of the array: %d\n", *arrayPtr);
arrayPtr++;
printf("Step 2: Second element of the array: %d\n", *arrayPtr);
arrayPtr++;
printf("Step 2: Third element of the array: %d\n", *arrayPtr);
arrayPtr = numbers;
*arrayPtr = 99;
printf("Step 2: Updated first element of the array: %d\n", numbers[0]);
// Dynamic Memory Allocation and Dereferencing
int size = 5;
int *dynamicArray = (int *)malloc(size * sizeof(int));
if (dynamicArray == NULL) {
printf("Step 3: Memory allocation failed.\n");
return 1;
}
for (int i = 0; i < size; i++) {
dynamicArray[i] = i * 5;
}
printf("Step 3: Dynamically allocated array: ");
for (int i = 0; i < size; i++) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
dynamicArray[2] = 42;
printf("Step 3: Updated value in the array: %d\n", dynamicArray[2]);
free(dynamicArray);
// Data Structures and Dereferencing
struct Node *head = NULL;
struct Node *second = NULL;
struct Node *third = NULL;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("Step 4: Linked List: ");
struct Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
// Function Pointers and Dereferencing
int (*funcPtr)(int, int);
funcPtr = add;
int result = (*funcPtr)(2, 3);
printf("Step 5: The result of the function call: %d\n", result);
// Passing Parameters to Functions
int x = 5;
int y = 10;
printf("Step 6: Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("Step 6: After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output.
sqlStep 1: Value stored at the memory location pointed to by 'ptr' is: 42
Step 2: First element of the array: 10
Step 2: Second element of the array: 20
Step 2: Third element of the array: 30
Step 2: Updated first element of the array: 99
Step 3: Dynamically allocated array: 0 5 10 15 20
Step 3: Updated value in the array: 42
Step 4: Linked List: 1 -> 2 -> 3 -> NULL
Step 5: The result of the function call: 5
Step 6: Before swapping: x = 5, y = 10
Step 6: After swapping: x = 10, y = 5
Let's consider another scenario.
Scenario: Arrays and Pointer Arithmetic
In this scenario, we'll work with arrays and demonstrate how dereferencing pointers and pointer arithmetic can be used to access and manipulate array elements.
Here's an example:
c#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int *ptr = numbers; // 'ptr' points to the first element of the array
// Dereference 'ptr' and print the first element
printf("First element of the array: %d\n", *ptr);
// Use pointer arithmetic to access and print the second element
ptr++;
printf("Second element of the array: %d\n", *ptr);
// Use pointer arithmetic to access and print the third element
ptr++;
printf("Third element of the array: %d\n", *ptr);
// Reset 'ptr' to the first element using pointer arithmetic
ptr = numbers;
// Modify the first element through dereferencing
*ptr = 99;
// Print the updated first element
printf("Updated first element of the array: %d\n", numbers[0]);
return 0;
}
In this code, we have an integer array numbers containing five elements. We declare a pointer ptr and initialize it to point to the first element of the array. Here's what each part of the code does:
1. The first printf statement dereferences ptr to print the first element of the array, which is 10.
2. We use pointer arithmetic (ptr++) to move ptr to the second element, and the next printf statement dereferences it to print the second element, which is 20.
3. Similarly, we increment ptr again to access and print the third element, which is 30.
4. We reset ptr to point back to the first element, and then we dereference it to modify the value at that location, changing the first element to 99.
5. Finally, we print the updated first element of the array, which is now 99.
Output :
sqlFirst element of the array: 10
Second element of the array: 20
Third element of the array: 30
Updated first element of the array: 99
Let's consider another example.
Scenario: Dynamic Memory Allocation and Dereferencing
In this scenario, we'll focus on dynamic memory allocation and how dereferencing pointers is essential for working with dynamically allocated memory. We'll allocate memory for an array, populate it with values, and then use dereferencing to access and modify those values.
c#include <stdio.h>
#include <stdlib.h>
int main() {
int size = 5;
int *dynamicArray = (int *)malloc(size * sizeof(int));
if (dynamicArray == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Initialize the dynamically allocated array
for (int i = 0; i < size; i++) {
dynamicArray[i] = i * 5;
}
// Dereference and print the values
printf("Dynamically allocated array: ");
for (int i = 0; i < size; i++) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
// Dereference and modify a value in the array
dynamicArray[2] = 42;
// Dereference and print the updated value
printf("Updated value in the array: %d\n", dynamicArray[2]);
// Free the dynamically allocated memory
free(dynamicArray);
return 0;
}
In this example, we perform the following steps:
1. Allocate dynamic memory for an array of integers using malloc. If the allocation fails, we print an error message and exit the program.
2. Initialize the elements of the dynamically allocated array through a loop, where each element is set to a multiple of 5.
3. Dereference the pointer dynamicArray to access and print the values in the array.
4. Dereference the pointer again to modify the value at index 2 of the array, changing it to 42.
5. Dereference and print the updated value in the array.
6. Finally, free the dynamically allocated memory using the free function to release the memory back to the system.
Output :
cDynamically allocated array: 0 5 10 15 20
Updated value in the array: 42
0 Comments