diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c8d388bd5e..36038cbf3d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5390,8 +5390,13 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { } if has_none_zero { init_str += '}' - type_name := g.typ(typ) - init_str = '($type_name)' + init_str + type_name := if info.is_anon { + // No name needed for anon structs, C figures it out on its own. + '' + } else { + '(${g.typ(typ)})' + } + init_str = type_name + init_str } else { init_str += '0}' } diff --git a/vlib/v/tests/struct_test.v b/vlib/v/tests/struct_test.v index bf6a7e5eca..d73abf40fa 100644 --- a/vlib/v/tests/struct_test.v +++ b/vlib/v/tests/struct_test.v @@ -425,6 +425,9 @@ struct Book { } fn test_anon() { - // book := Book{author:struct{'sdf', 23}} - // println(book.author.age) + empty_book := Book{} // author:struct{'sdf', 23}} + assert empty_book.author.age == 0 + assert empty_book.author.name == '' + + println(empty_book.author.age) }