Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following structure: struct Data { int n; char* name; int* arr[3]; }; n 33 name ? 7 0 0 0 0 arr -

image text in transcribedimage text in transcribedimage text in transcribed

Given the following structure: struct Data { int n; char* name; int* arr[3]; }; n 33 name ? 7 0 0 0 0 arr - 0 0 0 0 0 0 0 0 Which of the following code fragments dynamically allocate the memory as shown in the diagram above? 1) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); for (int i = 0; i arr[i] = malloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + j) = 0; 2) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); if (sptr->name == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int i = 0; i arr[i] = malloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + i) = 0; } 3) struct Data *sptr = malloc(sizeof(struct Data)); if (sptr == NULL) { printf("Unable to allocate memory. "); exit(1); } sptr->n = 33; sptr->name = malloc(12 * sizeof(char)); if (sptr->name == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int i = 0; i arr[i] = malloc(2 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. "); exit(1); } for (int j = 0; j arr[i] + 1) = 0; } for (int i = 0; i arr[i] = realloc(4 * sizeof(int)); if (sptr->arr[i] == NULL) { printf("Unable to allocate memory. In"); exit(1); } } 0 1 and 2 0 2 and 3 O 2 only O 3 only O1 only

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

ISBN: 061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago