In C arrays are not assignable, but in line 36 (line that I have also commented on) I assign a value to the array name without getting any errors. Why is this happening? Also, aside from this baffling thing I would really appreciate if you checked whether my freeStudents function is working properly. Thanks for your time guys!
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 50
struct students
{
char name[MAX_NAME];
float average;
};
void storeStudents(struct students *lst, int n);
void printStudents(struct students *lst, int n);
void freeStudents(struct students *lst);
int main(void)
{
int n;
printf("How many students you wanna store? ");
scanf("%d", &n);
struct students *list;
list = (struct students *)malloc(n*sizeof(struct students));
storeStudents(list,n);
printStudents(list,n);
freeStudents(list);
return 0;
}
void storeStudents(struct students *lst, int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Name of student: ");
scanf("%s", &(lst[i].name)); //In C arrays are not assignable, so why is this line working?
printf("Average of student: ");
scanf("%f", &(lst[i].average));
}
printf("\n");
}
void printStudents(struct students *lst, int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Name: %s\tAverage: %.2f", lst[i].name, lst[i].average);
printf("\n");
}
}
void freeStudents(struct students *lst)
{
free(lst);
}