Yes, this is bad practice; Even worse, you are sending output to stdout , not to stderr . This can lead to data corruption by mixing the error message with the output.
Personally, I am very sure that such "error handling" is harmful. You cannot confirm that the caller has passed a valid value for L , so the validation of index is inconsistent. The documented interface contract for the function should simply be that L should be a valid pointer to an object of the correct type, and index valid index (in any sense, it makes sense for your code). If an invalid value is passed for L or index , this is an error in the code, not a valid error that may occur at run time. If you need help debugging it, the assert macro from assert.h is probably a good idea; this makes it easy to disable validation when you no longer need it.
One of the possible exceptions to the above principle will be the case when the value of L comes from other data structures in your program, but index comes from some external input that is not under your control. Then you can perform the external verification step before calling this function, but if you always need verification, it makes sense to integrate it as you do. However, in this case, you need to report the caller’s failure, and not print the useless and malicious stdout message. Thus, you need to either reserve one possible return value as an error notification, or have an additional argument that allows you to return the result and error status to the caller.
source share