Array initialization in C — "cannot convert '<brace-enclosed initializer list>' to 'float' in assignment" error
10,655
rho[10] = ...
That is not an initializer. It is an assignment statement. And an invalid one at that as rho[10]
is a single array element.
An initializer very specifically refers to an assignment that is part of the variable declaration. So just change to:
float rho[] = { 0.1 , 0.4 , 0.5 , 0.6 , 0.7 , 0.74 , 0.78 , 0.8 , 0.85 , 0.9 } ;
Note that I have also removed the array size. Better to let the compiler work that out for you (unless you want an array larger than the number of initializer elements).
Author by
erengsgs
Updated on June 05, 2022Comments
-
erengsgs almost 2 years
I am trying to compile a code for my simulation project and I tried to initialize and represent my
rho
array but I encountered the initializer error:cannot convert 'brace-enclosed initializer list' to 'float' in assignment.
I need to assign
rho[0]
to the first member of the array and so on.#include <stdio.h> #include <time.h> #include <stdlib.h> #include <math.h> int main() { float lambda; // ratio of customers and time float N; // mean number of customers in the system float Q; // mean number of customers in queue float res_time; // response time float mu; // service rate , assuming mu = 10 customers/s for this assignment int i; float rho[10]; // array for rho values rho[10] = { 0.1, 0.4, 0.5, 0.6, 0.7, 0.74, 0.78, 0.8, 0.85, 0.9 }; printf("\nPlease enter the mu : "); scanf("%f", &mu); FILE *f1, *f2, *f3, *f4, *f5, *f6, *f7, *f8, *f9, *f10; if (i == 0) { N = rho[i] / (1 - rho[i]); lambda = rho[i] * mu; Q = (i * rho[i]) / (1 - rho[i]); res_time = N / lambda; printf("%.4f \t\t %.4f \t\t %.4f \t\t %.4f \n", rho[i], N, Q, res_time); f1 = fopen("NvsRho[0.1].txt", "w"); if (f1 == NULL) { printf("Cannot open file.\n"); exit(1); } fprintf(f1, "RHO \t\t N \n--- \t\t ---\n"); fprintf(f1, "%.4f \t\t %.4f \n", i, N); fclose(f1); } return 0; }
-
erengsgs over 7 yearsWhen I try this, " [Error] expected primary-expression before ']' token " error is printed. @kaylum
-
kaylum over 7 years@erengsgs Please show your updated code in the question. You may have made an error. Please leave the original code and add the new code below it to keep the question history.
-
erengsgs over 7 yearsNow I fixed the problem. Thank you so much for your constructive comment. In this site most of the people dont understand that this is impossible that everybody is a code master. Thank you again. @kaylum
-
artm over 7 years@erengsgs common practise is to accept an answer as it works for you