daddinuz / error

Immutable singleton instance to represent errors.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error

Immutable singleton instance to represent errors.

#include <stdio.h>
#include <error.h>

const struct Error *divide(const double dividend, const double divisor, double *const out) {
    if (NULL == out) {
        return NullPointerError;
    } else if (0 == divisor) {
        return MathError;
    } else {
        *out = dividend / divisor;
        return Ok;
    }
}

int main() {
    double result = 0;
    const struct Error *error = divide(5, 0, &result);
    (Ok == error) ? printf("Result: %f\n", result) : printf("Error: %s\n", error->message);
    return 0;
}

About

Immutable singleton instance to represent errors.

License:MIT License


Languages

Language:C 86.9%Language:CMake 10.4%Language:Shell 2.7%