" " ISO C ( -std=c99), gcc . , gcc, : -Wmissing-field-initializers, , AFAIK, , -Wextra, , :
int main ( void )
{
foo bar = {123, "foobar"};
foo zar = {123, '\a'};
return 0;
}
:
warning: initialization makes integer from pointer without a cast [enabled by default]
foo bar = { 123, "foobar"};
warning: missing initializer for field ‘c’ of ‘foo’ [-Wmissing-field-initializers]
foo zar = { 123, 'a'};
, , , ... IMO...
, , . , char *, :
foo * init_foo(foo *str, int a, char b, char *c)
{
if (c == NULL) return NULL;
if (str == NULL) str = malloc(sizeof *str);
str->a = a;
str->b = b;
str->c = calloc(strlen(c) + 1, sizeof *(str->c));
strcpy(str->c, c);
return str;
}
void free_foo ( foo **f)
{
if ((*f)->c != NULL) free((*f)->c);
free(*f)
*f = NULL;
}
... :
foo *new_foo = init_foo( NULL, 123, '\a', "string");
free_foo(&new_foo);
foo stack_foo;
init_foo(&stack_foo, 123, '\a', "string");
free(stack_foo.c);
stack_foo.c = NULL;
, , -Wall , --pedantic, ...