Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = NULL;
// Safe check before dereferencing
if (ptr == NULL) {
printf("Pointer is NULL, cannot access memory\n");
} else {
printf("Value = %d\n", *ptr);
}
// Allocate memory safely
ptr = (int *)malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
*ptr = 100;
printf("Value after allocation = %d\n", *ptr);
free(ptr);
ptr = NULL; // avoid dangling pointer
return 0;
}Coding works best on desktop or with an external keyboard.