I have the following function where I'm trying to pass a NULL to emulate an "optional" parameter:
char * slice2(const char * str, const unsigned int * start, const unsigned int * end) {
size_t string_len = strlen(str);
printf("Start: %d | End: %d\n", *start, *end);
if (*start == NULL) inferred_start = 0;
if (*end == NULL) inferred_end = string_len;
// more code and such
}
And called as:
unsigned int start = 6;
printf("'%s' ==> %s\n", "String", slice2("String", &start, NULL));
What would be the proper way to do the above?
returnstatement. This will likely trigger an illegal instruction error if it even compiles.char * slice2(const char * str, const size_t * start, const size_t * end) { size_t begin = (start == NULL ? 0 : *start); return (end == NULL ? strdup(str + begin) : strndup(str + begin, *end - begin)); }godbolt.org/z/DUINJi