Question: In C, write a simple program to define a struct template and define it as a datatype using typedef. Than allocate a block of dynamically

In C, write a simple program to define a struct template and define it as a datatype using typedef. Than allocate a block of dynamically allocated memory and set the values for the different members. Than have them print out the contents.

When running the code below I get these 2 errors. Any help is appreciated

1. return type of main is not int

2. must use struct tag to refer to type patient

#include

#include //for malloc

#include

struct patient

{ char name[20]; int age; float weight; float height; int pulse_rate; };

void main()

{

typedef patient Patient; //typedef definition used now we have new variable type Patient

Patient *P; // Patient type pointer variable declared

P=(Patient*)malloc(50*sizeof(Patient)); //dynamically allocating memory by malloc

if(P==NULL)

{ printf("couldn't allocate memory ");

}

else

{

strcpy(P->name,"Daniel"); //directly asigning values to the members

P->age=34;

P->weight=180.0;

P->height=70.3;

P->pulse_rate=60;

}

printf("Details are: ");

printf("name:%s ",P->name);

printf("age:%d ",P->age);

printf("pulse_rate:%d ",P->pulse_rate);

printf("height:%f ",P->height);

printf("weight:%f ",P->weight);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!