Hello guys! Firstly, in C arrays are not assignable so why is the following command (in line 21) not an error?
scanf("%s", lst[i].name);
Moreover making the following two commands shows error: assignment to expression with array type
struct people michael;
michael.name = "michael";
Theoretically since the first command (the scanf) does not show any errors it should be safe to assume that the two commands above should not show any errors either. But this is not the case. The full program is shown bellow (without the troubling commands above). The two questions I have are:
What exactly is happening behind the scenes when we make the first command (regarding the very baffling lst[i].name)?
What is the difference between the first command and the two troubling ones right above? In my mind the same mistake happens in both occasions (assigning to an array).
#include <stdio.h>
#include <stdlib.h>
#define MAX_LETTERS 50
struct people
{
char name[MAX_LETTERS];
};
int main()
{
int number_of_people,i;
printf("what is the number of people you wanna store?");
scanf("%d", &number_of_people);
struct people *lst;
lst = (struct people *)malloc(number_of_people * sizeof(struct people));
for(i=0; i<number_of_people; i++)
{
scanf("%s", lst[i].name); //how can this be a valid assignment?
}
for(i=0; i<number_of_people; i++)
{
printf("%s", lst[i].name);
printf("\n");
}
free(lst);
return 0;
}