...
This compliant solution changes the interface of getlen()
to store the result in a user-provided pointer and return an error code to indicate any error conditionsa status indicator to report success or failure. The best method for handling this type of error is specific to the application and the type of error (see ERR00-C. Adopt and implement a consistent and comprehensive error-handling policy for more on error handling).
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stddef.h> int getlen(const int *input, size_t maxlen, int delim, size_t *result) { if (result == NULL) { return -1; } for (size_t i = 0; i < maxlen; ++i) { if (input[i] == delim) { if (result != NULL) { *result = i; } return 0; } } return -1; } void func(int userdata) { size_t i; int data[] = {1, 1, 1}; if (getlen(data, sizeof(data), 0, &i) != 0) { /* Handle error */ } else { data[i] = userdata; } } |
...