We use the abort_handler_s
function to write an error message on the defined format on the standard error stream stderr
. We use this as a parameter of the function set_constraint_handler_s
.
It outputs it to stderr
, which is displayed on the terminal.
After output to stdout, abort_handler_s
calls the abort function.
#include<stdlib.h>
void abort_handler_s
(
const char * restrict message,
void * restrict temp,
errno_t error
);
message
- A pointer to the message which describes the error.
temp
- A pointer to the data given.
error
- Error code.
This function has no return value.
#include <string.h>#include <stdio.h>#include <stdlib.h>int main(void){char* ptr = NULL;set_constraint_handler_s(abort_handler_s);gets_s(ptr, 9);return 0;}
In this piece of code, we initialize a pointer to an array and store NULL
in it.
Then, we call the set_constraint_handler_s
function and pass the abort_handler_s
as a parameter.
Next, we call the gets_s
function.
The benefit of using
gets_s
function is that no matter how many characters the user inputs, only the first 9 characters are stored in the ptr.
Free Resources