Tiffany Tiffany - 1 year ago 52
C Question

Issue when initializing a double-typed array by zero

I want to create an array of type

and initialized it by
. I did the following:

double sum[3];

sum = {0.0};
for (int i = 0; i < 3; ++i) {
printf("%f ", sum[i]);

However, the compiler says it is wrong
error: expected expression
! Why? and how to fix it?

Answer Source

You're not initializing, you're assigning the value. Moreover, you cannot assign anything to an array type, at all.

The LHS operand of an assignment operator should be a modifiable lvalue, and an array (array name) is not a modifiable lvaule. So you cannot assign anything to an array.

Try something like

double sum[3] = {0.0};