Question

In: Computer Science

Write C Code for the following question which has two parts: a) Show how a typedef...

Write C Code for the following question which has two parts:

a) Show how a typedef statement can be used to declare a new data type – to demonstrate this create a new type called Person which is the structure declared previously.

b) Write C code to declare an array of 50 pointers to Persons. Using a loop, use malloc to dynamically allocate memory for each of these structures. There is no need to put any values into the structures. Use a second loop to then deallocate each structure.

Solutions

Expert Solution

a)

The structure declared which is represented by typedef is provided below:

typedef struct Person;

Explanation:

  • The typedef is used to define the name of the variable, structure etc.
  • For the structure Person, the typedef statement is provided above.

b)

The declaration of array of 50 pointers to Person structure is as follows:

struct person *Person;

Using for loop, allocation of memory using malloc is provided below:

for(int i = 0; i < 50; i++)

{

    Person = (struct Person*)malloc(50 * sizeof(struct Person));

}

Using second for loop, deallocation of the structure is provided below:

for(int i = 0; i < 50; i++)

{

    free(Person);

}

The complete code of C programming is provided below:

Screenshot of the code:

Code To Copy:

//Include the header file.

#include <stdio.h>

#include <malloc.h>

//Define the structure Person.

struct Person{

};

//Define the main function.

int main(void) {

//Declare an pointer of structure Person.

struct person *Person;

//Allocate memory for 50 persons using malloc.

for(int i = 0; i < 50; i++)

{

    Person = (struct Person*)malloc(50 * sizeof(struct

Person));

}

//Deallocate the memory for 50 persons.

for(int i = 0; i < 50; i++)

{

    free(Person);

}

}


Related Solutions

I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
1 Draw a stack diagram to show how the following code is executed and write the...
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10)) 2 Draw a stack diagram to show how the following code is executed...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
Write a C++ code using while loop which is getting the two integers from the user,...
Write a C++ code using while loop which is getting the two integers from the user, and output how many numbers are multiples of 5, and how many numbers are multiples of 7 between the two integers (inclusive).
Write a code in C that will take four words from the user and show those...
Write a code in C that will take four words from the user and show those in ascending order.
Question 1 (this question has three parts, (a), (b), and (c)) (a) As a response to...
Question 1 (this question has three parts, (a), (b), and (c)) (a) As a response to the recent COVID-19 outbreak, the Commonwealth Government put in place lockdown restrictions. Using the dynamic AD-AS framework, analyse and demonstrate the impact of the COVID-19 pandemic on the level of output (or real GDP), unemployment, and inflation. [4+4 marks ] (b) In response to the COVID-19 pandemic, in March 2020 the Commonwealth Government announced a fiscal stimulus which included income support for workers and...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2) 2. calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 2 and 20 (inclusive)...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 (Number1 should be less than number2) 2. Calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 1 and 20 (inclusive) 6. Calculate and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT