Complete the code to check if the pointer is NULL before using it.
if ([1] != NULL) { printf("Pointer is valid.\n"); }
We check if the pointer ptr is not NULL before using it to avoid errors.
Complete the code to safely open a file and check if it opened successfully.
FILE *file = fopen("data.txt", "r"); if ([1] == NULL) { printf("Failed to open file.\n"); }
fopen directly instead of the file pointer variable.We check if file is NULL to know if fopen failed.
Fix the error in the code to prevent buffer overflow by limiting input size.
char buffer[10]; printf("Enter text: "); scanf("%[1]s", buffer);
Using %9s limits input to 9 characters plus the null terminator, preventing overflow.
Fill both blanks to safely allocate memory and check if allocation succeeded.
int *arr = (int *)malloc([1] * sizeof(int)); if (arr [2] NULL) { printf("Memory allocation failed.\n"); }
Allocate memory for 10 integers and check if arr is NULL to detect failure.
Fill all three blanks to safely copy a string with length check.
char src[] = "Hello"; char dest[6]; if (strlen(src) [1] sizeof(dest) - 1) { strncpy(dest, src, [2]); dest[[3]] = '\0'; } else { printf("Source string too long.\n"); }
Check if source length is less or equal to destination size minus 1, copy that many characters, and add null terminator at last index.