C Access Memory
Accessing dynamically allocated memory involves using pointers to refer to the allocated memory block. Pointers can be dereferenced to read or modify values stored at the memory location.
Key Topics
1. Accessing Memory via Pointers
Example: Reading and Writing Values
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
*ptr = 100; // Write value
printf("Stored value: %d\n", *ptr); // Read value
free(ptr);
return 0;
}
Code Explanation: A dynamically allocated memory block is accessed using a pointer. The *
operator dereferences the pointer to read or write the memory.