error handling strategies in C?

Posted by Leo on Stack Overflow See other posts from Stack Overflow or by Leo
Published on 2010-05-03T01:11:52Z Indexed on 2010/05/03 1:18 UTC
Read the original article Hit count: 234

Filed under:

Given the code below:

typedef struct {int a;} test_t;

arbitrary_t test_dosomething(test_t* test) {
    if (test == NULL) {
        //options:
        //1. print an error and let it crash
          //e.g. fprintf(stderr, "null ref at %s:%u", __FILE__, __LINE__);
        //2. stop the world
          //e.g. exit(1);
        //3. return (i.e. function does nothing)
        //4. attempt to re-init test
    }
    printf("%d", test->a); //do something w/ test
}

I want to get a compiler error if test is ever NULL, but I guess that's not possible in C. Since I need to do null checking at runtime, what option is the most proper way to handle it?

© Stack Overflow or respective owner

Related posts about c