Итак, это мой код:Использование таНос с структуры в C
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
struct person{
char name[18];
int age;
float weight;
};
int main()
{
struct person *personPtr=NULL, person1, person2;
personPtr=(struct person*)malloc(sizeof*(struct person));
assert (personPtr!=NULL);
personPtr = &person1; // Referencing pointer to memory address of person1
strcpy (person2.name, "Antony"); //chose a name for the second person
person2.age=22; //The age of the second person
person2.weight=21.5; //The weight of the second person
printf("Enter a name: ");
personPtr.name=getchar(); //Here we chose a name for the first person
printf("Enter integer: ");
scanf("%d",&(*personPtr).age); //Here we chose the age of the first person
printf("Enter number: ");
scanf("%f",&(*personPtr).weight); //Here we chose the weithgt of the first person
printf("Displaying: "); //Display the list of persons
printf("\n %s, %d , %.2f ", (*personPtr).name, (*personPtr).age,(*personPtr).weight); //first person displayed
printf("\n %s, %d , %.2f",person2.name, person2.age, person2.weight); //second person displayed
free(personPtr);
return 0;
}
Я получаю две ошибки, и я не знаю, почему. Во-первых, я не думаю, что я выделил право памяти, то первая ошибка на следующей строке:
personPtr=(struct person*)malloc(sizeof*(struct person));
Он говорит, что:
[Ошибка] Ожидается выражение до «)» маркера
Вторая ошибка, которую я получаю на линии
personPtr.name=getchar();
Почему я не могу попу Запустить имя, используя getchar для структуры? Ошибка:
[Error] запрос 'имя' члена в чем-то не структуры или объединения
'personPtr = таНос (SizeOf (структура человека)) и' personPtr-> имя [0] = GetChar(); ' – MayurK