I can't find the problem to this test.
I've already allocated memory space for the array, and I can't access to it from the pointer by using '->'
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TOTAL 3
int main() {
typedef struct {
char name[20];
int age;
} Person;
Person *persons;
persons = (Person *) malloc(TOTAL * sizeof(Person));
strcpy(persons[0]->name = "Ray");
persons[0]->age = 4;
printf("%s, %d", persons[0]->name, persons[0]->age);
/*
for (int i=0; i < TOTAL; i++) {
printf("// %d\n", i);
printf("name: "); scanf("%20s", persons[i]->name);
printf("age: "); scanf("%d", &persons[i]->age);
printf("- - - -\n\n");
}
for (int i=0; i < TOTAL; i++) {
if (persons[i]->age > 21) {
printf("%s, %d anios.\n", persons[i]->name, persons[i]->age);
}
}
*/
free(persons);
fflush(stdin);
getchar();
return 0;
}
This is my output:
Code:
D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c: In function 'int main()':
D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:17:19: error: base operand of '->' has non-pointer type 'main()::Person'
D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:18:12: error: base operand of '->' has non-pointer type 'main()::Person'
D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:19:29: error: base operand of '->' has non-pointer type 'main()::Person'
D:\arbol\documentos\qwerty\taller lenguaje 1\asdf\struct_malloc.c:19:47: error: base operand of '->' has non-pointer type 'main()::Person'
[Finished in 0.3s with exit code 1]