Write a C program that contains a structure that uses predef

Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adult), and kindOfPerson (Either kid, student, or adult) • kids have a school field. Students have college and gpa. Adults have company and salary. • Create one non-dynamic struct with the content for a college student: \"Bob\", 20, K-State, 3.5 • Create one struct dynamically for a kid with: \"Alison\", 10, \"Amanda Arnold Elementary\" • Implement a function that can be used to display the contents of a single structure • Print out both structures • Free up the memory allocated before exiting

Solution

Simple structure

#include<stdio.h>

#include<string.h>

struct str1

{

char name[20];

int age;

char kind[20];

char kindOfPerson[20]

};

Non dynamic struct :

struct student

{

char name[20];

int age;

char college[30];

double salary;

};

int main()

{

struct student record = {0}; //initializing null

strcpy(record.name,\"Bob\");

record.age=20;

strcpy(record.college,\"K-State\");

record.salary=3.5;

printf(\" Name is: %d \ \", record.name);

printf(\" Age is: %d \ \", record.age);

printf(\" College is: %d \ \", record.college);

printf(\" Salary is: %d \ \", record.salary);

}

Dynamic structure:

#include<stdio.h>

#include<stdlib.h>

Implementing function

For ex if we want to display the contents of structure student then we can write a function as shown below :

{

//print details

}

Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adul
Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site