While this works, the p1 object has global scope, which is probably overkill. You wouldn't need personPtr at all.
If the point of the exercise was to use pointers, it makes more sense to write something like this instead, since you are using pointers for a reason.
Code:
#include <stdio.h>
#include <stdlib.h>
struct person
{
int age;
float weight;
};
int main(void)
{
struct person *personPtr = malloc(sizeof *personPtr);
/* PersonPtr still holds the address of a struct object, except now that makes more sense because
otherwise we would have no way of using the memory returned by malloc.
*/
if (personPtr != NULL)
{
personPtr->age = 30;
personPtr->weight = 56;
printf("Age: %d\n", personPtr->age);
printf("weight: %f\n", personPtr->weight);
/* Clean memory and prepare pointer for next use */
free(personPtr);
personPtr = NULL;
}
return 0;
}