You have several problems:
-
Format specifiers start with a
%sign. So changescanf("d", &element[j].age);to
scanf("%d", &element[j].age);and
scanf("s", &element[j].name);to
scanf("%s", element[j].name);Wondering why I removed the
&from the abovescanf? It is because array names gets converted to a pointer to its first element. -
These
printf("%d. Fav number: %d\n", k, &element[k].age); printf("%d. Fav word: %s\n", k, &element[k].name);should be
printf("%d. Fav number: %d\n", k, element[k].age); printf("%d. Fav word: %s\n", k, element[k].name);because the
%din theprintfexpects the value(int) and not the address of the variable(int*). Similarly,%sexpects achar*, not achar(*)[20] -
The cast here is completely wrong and should be removed:
element[j] = (struct data) malloc(sizeof(struct data));This is done because
mallocreturnsvoid*which can be assigned to any pointer type without casting. -
-
- etc See other answers.
-
solved Structs in C Error [closed]