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
#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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started