Goal unclear
OP has "I want a 'view' not a copy.", yet uses malloc to make a copy.
Lacking documentation
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalGoal unclear
OP has "I want a 'view' not a copy.", yet uses malloc to make a copy.
Lacking documentation
Lacking documentation
Goal unclear
OP has "I want a 'view' not a copy.", yet uses malloc to make a copy.
Lacking documentation
It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found. This is a bug for an array function.
A helper function that existsexits!?
Use an auto formatformatter.
Recall size_t is an unsigned type and i - *scan_from can become a large value near SIZE_MAX.
I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *
In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead for an array function.
It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found.
A helper function that exists!?
Use an auto format.
Recall size_t is an unsigned type and i - *scan_from become a large value near SIZE_MAX.
I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *
In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.
It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found. This is a bug for an array function.
A helper function that exits!?
Use an auto formatter.
Recall size_t is an unsigned type and i - *scan_from can become a large value near SIZE_MAX.
I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use.
In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead for an array function.
Poor for code to have a buried memory allocation yet does not advise that the caller should free it.
"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.
Document at a high level as if caller has access to the function declaration and not the definition. (in the .h file)
Array or string?
exit(ENOMEM) is surprising and undocumented in function description. Further @Mr R Further, ENOMEM is not part of the standard C lib and reduces portability.
// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len >= 0 && (unsigned) len == substr_length); const return?
I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *
Bug
In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.
"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.
Array or string?
exit(ENOMEM) is surprising and undocumented. Further, ENOMEM is not part of the standard C lib and reduces portability.
// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len == substr_length); Poor for code to have a buried memory allocation yet does not advise that the caller should free it.
"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.
Document at a high level as if caller has access to the function declaration and not the definition. (in the .h file)
Array or string?
exit(ENOMEM) is surprising and undocumented in function description. @Mr R Further, ENOMEM is not part of the standard C lib and reduces portability.
// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len >= 0 && (unsigned) len == substr_length); const return?
I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *
Bug
In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.