Hi, I'm starting programming, but I can't make this code work, does anyone know why it could be? is a structure with a for loop to display the data, I get this error "expected expression before '{' token" in all the lines of the subject information
#include<stdio.h>
#include<stdlib.h>
struct datosMaterias{
char *nombreMateria;
char *carrera;
int horasSemana;
int horasLaboratorio;
};
void materias(){
struct datosMaterias materia[9];
materia[0] = {"Ingles","Desarrollo xd",20,10};
materia[1] = {"Sistemas","Desarrollo de software",10,15};
materia[2] = {"Estadistica","Desarrollo de software",10,5};
materia[3] = {"Fundamentos de programacion","Desarrollo de software",10,5};
materia[4] = {"Base de datos","Desarrollo de software",10,1};
materia[5] = {"Fisica","Desarrollo de software",15,5};
materia[6] = {"Quimica","Desarrollo de software",10,8};
materia[7] = {"Calculo diferencial","Desarrollo de software",5,7};
materia[8] = {"Matematicas","Desarrollo de software",9,9};
system("cls");
int i=0;
for(i=0;i<9;i++){
printf("Nombre: %s \n Carrera: %s \n Numero de horas por semana: %i \n Horas de laboratorio: %i \n",materia[i].nombreMateria,materia[i].carrera,materia[i].horasSemana,materia[i].horasLaboratorio);
printf("____\n");
}
system("pause");
}
int main(){
materias();
return 0;
}
The problem is simple, on line 10, when you initialize the first element of the structure array, you do it using keys, however this type of initialization is only allowed in the variable declaration, so the compiler tells you an error; if you want to initialize your variable in this way, you must do it in the declaration, as shown below.
That should solve your problem. On the other hand, if you don't want/can't initialize your array this way, what you can do is initialize element by element, member by member.