So, this is my code:
#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;
}
I get two errors and I don't know why. Firstly, I don't think that I allocated the memory right, the first error is on the next line:
personPtr=(struct person*)malloc(sizeof*(struct person));
It says that:
[Error] expected expression before ')' token
The second error that I get is on the line
personPtr.name=getchar();
Why cant I assign a name using getchar for a structure? The error is:
[Error] request for member 'name' in something not a structure or union
personPtr=malloc(sizeof(struct person));andpersonPtr->name[0]=getchar();