Skip to content

Commit

Permalink
Extend STR_NULL and str_init() to set a type explicitly. This
Browse files Browse the repository at this point in the history
allows those macros to be used elsewhere in the function body,
not only in variable declaration(s), i.e.:

void foobar(int baz)
{
    str localstr;

    if (random() == 42) {
       localstr = str_init(I_STR LUCKY_STR FIN_STR);
    } else {
        localstr = str_init(I_STR UNLUCKY_STR FIN_STR);
    }
    some_api(&localstr);
}
  • Loading branch information
sobomax committed Oct 6, 2020
1 parent 9dcff69 commit 36e0281
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions str.h
Expand Up @@ -59,8 +59,8 @@ struct __str {
typedef struct __str str;

/* str initialization */
#define STR_NULL {NULL, 0}
#define str_init(_string) {_string, sizeof(_string) - 1}
#define STR_NULL (str){NULL, 0}
#define str_init(_string) (str){_string, sizeof(_string) - 1}
static inline void init_str(str *dest, const char *src)
{
dest->s = (char *)src;
Expand Down

0 comments on commit 36e0281

Please sign in to comment.