Tiffany Tiffany - 2 months ago 6
C Question

Issue when initializing a double-typed array by zero

I want to create an array of type

double
and initialized it by
0.0
. 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

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};
Comments