Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that contains a structure that uses predefined types and union

ID: 3792226 • Letter: W

Question

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

Explanation / Answer

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

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote