Я пытаюсь прочитать произвольное количество строковых элементов в массиве структур при инициализации моей программы. Я хочу выделить кучу памяти для
Когда компилятор попадает в следующую строку, он выдает error: invalid initializer
.
Первая часть моего кода:
int main() {
printf("Work starts in the vineyard.\n");
typedef struct {
char* name[20];
unsigned int jobs;
}Plantation;
// read from list of plantations
FILE *plantationFile = fopen("./data/plantations.txt", "r");
if (plantationFile==NULL) {perror("Error opening plantations.txt."); exit(1);}
char line[20];
char *lp = line;
int plantationCount;
Plantation plantations[] = (Plantation *)malloc(sizeof(Plantation));
if (!feof(plantationFile)) {
int i = 0;
fgets(line, 20, plantationFile);
scanf(lp, "%i", &plantationCount);
realloc(plantations, sizeof(Plantation) * plantationCount);
while( !feof(plantationFile) ) {
fgets(line, 20, plantationFile);
strcpy(*(plantations[i].name), lp);
plantations[i].jobs = 1u;
++i;
}
}
...
Чего мне здесь не хватает?
Вывод компилятора:
$ gcc -W -Wall vineyard.c
vineyard.c: In function ‘main’:
vineyard.c:30:32: error: invalid initializer
Plantation plantations[] = (Plantation *)malloc(sizeof(Plantation));
^
Он также выдает то же самое, если я пропущу приведение типов.
$ gcc -W -Wall vineyard.c
vineyard.c: In function ‘main’:
vineyard.c:30:32: error: invalid initializer
Plantation plantations[] = malloc(sizeof(Plantation));
^~~~~~
Спасибо!