diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 6240296d89..7e87f2d9ee 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -443,10 +443,17 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { // TODO Instead of the translated check, check if it's a pointer already // and don't generate memcpy & typ_str := g.typ(val_type).trim('*') - ref_str := if val_type.is_ptr() { '' } else { '&' } - g.write('memcpy((${typ_str}*)') + final_typ_str := if is_fixed_array_var { '' } else { '(${typ_str}*)' } + final_ref_str := if is_fixed_array_var { + '' + } else if val_type.is_ptr() { + '(byte*)' + } else { + '(byte*)&' + } + g.write('memcpy(${final_typ_str}') g.expr(left) - g.write(', (byte*)${ref_str}') + g.write(', ${final_ref_str}') g.expr(val) g.write(', sizeof(${typ_str}))') } else if is_decl { diff --git a/vlib/v/tests/fixed_array_test.v b/vlib/v/tests/fixed_array_test.v index 5fea6caac2..23e6eb2665 100644 --- a/vlib/v/tests/fixed_array_test.v +++ b/vlib/v/tests/fixed_array_test.v @@ -119,3 +119,22 @@ fn test_for_in_fixed_array() { arr := [1, 2, 3]! calc_size(arr) } + +// + +fn print_fixed_arr(a [2]int) { + println('a: ${a}') + assert a == [1, 2]! + tmp := a + println('tmp: ${tmp}') + assert tmp == [1, 2]! +} + +fn test_assignment_of_a_fixed_array_passed_as_parameter() { + arr := [1, 2]! + assert arr == [1, 2]! + tmp := arr + assert tmp == [1, 2]! + println(tmp) // [1, 2] + print_fixed_arr(arr) +}